Skip to content

Commit 7659ac6

Browse files
committed
chore(docs): improve comments here and there
1 parent 0d7a14b commit 7659ac6

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

book/content/part02/hash-map.asc

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A Map is a data structure where a `key` is mapped to a `value`. It's used for a
1111

1212
NOTE: Map has many terms depending on the programming language. Here are some other names: Hash Map, Hash Table, Associative Array, Unordered Map, Dictionary.
1313

14-
==== Map Application
14+
==== Map Applications
1515

1616
Maps are one of the most popular data structures because of their fast lookup time.
1717

book/content/part02/linked-list.asc

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ ifndef::imagesdir[]
33
:codedir: ../../../src
44
endif::[]
55

6-
[[linked-list]]
7-
=== Linked List
86
(((Linked List)))
97
(((List)))
108
(((Data Structures, Linear, Linked List)))
9+
[[linked-list]]
10+
=== Linked List
11+
1112
A list (or Linked List) is a linear data structure where each node is "linked" to the next.
1213

1314
.Linked Lists can be:

src/data-structures/linked-lists/linked-list.js

+18-20
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ class LinkedList {
1616
}
1717
// end::constructor[]
1818

19-
/**
20-
* Alias for size
21-
*/
22-
get length() {
23-
return this.size;
24-
}
25-
2619
// tag::addFirst[]
2720
/**
2821
* Adds element to the begining of the list. Similar to Array.unshift
@@ -92,25 +85,22 @@ class LinkedList {
9285
}
9386
// Adding element in the middle
9487
const current = this.get(position);
95-
if (current) {
96-
const newNode = new Node(value); // <3>
97-
newNode.previous = current.previous; // <4>
98-
newNode.next = current; // <5>
99-
100-
current.previous.next = newNode; // <6>
101-
current.previous = newNode; // <7>
102-
this.size += 1;
103-
return newNode;
104-
}
88+
if (!current) return undefined; // out of bound index
10589

106-
return undefined; // out of bound index
90+
const newNode = new Node(value); // <3>
91+
newNode.previous = current.previous; // <4>
92+
newNode.next = current; // <5>
93+
current.previous.next = newNode; // <6>
94+
current.previous = newNode; // <7>
95+
this.size += 1;
96+
return newNode;
10797
}
10898
// end::addMiddle[]
10999

110100
// tag::searchByValue[]
111101
/**
112102
* Search by value. It finds first occurrence of
113-
* the element matching the value.
103+
* the position of element matching the value.
114104
* Runtime: O(n)
115105
* @example: assuming a linked list with: a -> b -> c
116106
* linkedList.indexOf('b') // ↪️ 1
@@ -136,7 +126,8 @@ class LinkedList {
136126
* linkedList.get(1) // ↪️ 'b'
137127
* linkedList.get(40) // ↪️ undefined
138128
* @param {Number} index position of the element
139-
* @returns {Node} element at the specified position in this list.
129+
* @returns {Node|undefined} element at the specified position in
130+
* this list or undefined if was not found.
140131
*/
141132
get(index = 0) {
142133
return this.find((current, position) => {
@@ -302,6 +293,13 @@ class LinkedList {
302293
const parts = [...this]; // see [Symbol.iterator]()
303294
return parts.map((n) => util.inspect(n.node.value)).join(' -> ');
304295
}
296+
297+
/**
298+
* Alias for size
299+
*/
300+
get length() {
301+
return this.size;
302+
}
305303
}
306304

307305
// Aliases

src/data-structures/linked-lists/node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Node {
66
constructor(value) {
77
this.value = value;
88
this.next = null;
9-
this.previous = null;
9+
this.previous = null; // for doubly linked list
1010
}
1111
}
1212
// end::snippet[]

0 commit comments

Comments
 (0)