Skip to content

Commit 723e0b6

Browse files
committed
adding chapter 6 Linked, Double Linked and Circular List's
1 parent 2588ad5 commit 723e0b6

File tree

4 files changed

+332
-4
lines changed

4 files changed

+332
-4
lines changed

06-chapter-1-Linked-List.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
class Node {
2+
constructor(element = 'Head', next = null) {
3+
this.element = element;
4+
this.next = next;
5+
}
6+
}
7+
8+
class LList {
9+
constructor(head = new Node()) {
10+
this.head = head;
11+
this.current = this.head;
12+
}
13+
14+
find(item) {
15+
let currNode = this.head;
16+
while (currNode.element != item) {
17+
currNode = currNode.next;
18+
}
19+
return currNode;
20+
}
21+
22+
insert(newElement, item){
23+
const newNode = new Node(newElement);
24+
const current = this.find(item);
25+
newNode.next = current.next;
26+
current.next = newNode;
27+
}
28+
29+
findPrevious(item) {
30+
let currNode = this.head;
31+
while (!(currNode.next === null) && currNode.next.element != item) {
32+
currNode = currNode.next;
33+
}
34+
return currNode;
35+
}
36+
37+
remove(item) {
38+
let prevNode = this.findPrevious(item);
39+
if(!(prevNode.next === null)) {
40+
prevNode.next = prevNode.next.next;
41+
}
42+
}
43+
44+
display() {
45+
let currNode = this.head;
46+
while (!(currNode.next === null)) {
47+
console.log(currNode.next.element);
48+
currNode = currNode.next;
49+
}
50+
}
51+
52+
advance(n) {
53+
let position = n;
54+
while(!(this.current.next === null)) {
55+
if(n-- > 0) {
56+
this.current = this.current.next;
57+
} else if (this.current.next === null) {
58+
console.log(`Can't advanced ${position} position in the list`);
59+
break;
60+
} else {
61+
break;
62+
}
63+
}
64+
}
65+
66+
show() {
67+
console.log(this.current.element);
68+
}
69+
70+
}
71+
72+
// Implementation
73+
// ##########################################
74+
/*
75+
1. Implement the advance(n) function so that when executed, the current node is moved n nodes forward in the list.
76+
*/
77+
console.log('CHAPTER 6');
78+
console.log('### Excercise 1');
79+
80+
const cities = new LList();
81+
cities.insert("Conway", "Head");
82+
cities.insert("Russellville", "Conway");
83+
cities.insert("Carlisle", "Russellville");
84+
cities.insert("Alma", "Carlisle");
85+
cities.display();
86+
console.log('remove; Carlisle\n');
87+
cities.remove("Carlisle");
88+
89+
console.log('Linked List Elements:');
90+
cities.display();
91+
92+
console.log('\nAdvance 3 positions');
93+
cities.advance(3);
94+
console.log('Display current Node');
95+
cities.show();

06-chapter-2-Double-Linked-List.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
class Node {
2+
constructor(element = 'Head', next = null, prev = null) {
3+
this.element = element;
4+
this.next = next;
5+
this.prev = prev;
6+
}
7+
}
8+
9+
class DLList {
10+
constructor(head = new Node()) {
11+
this.head = head;
12+
this.current = this.head;
13+
}
14+
15+
find(item) {
16+
let currNode = this.head;
17+
while (currNode.element != item) {
18+
currNode = currNode.next;
19+
}
20+
return currNode;
21+
}
22+
23+
insert(newElement, item) {
24+
const newNode = new Node(newElement);
25+
const current = this.find(item);
26+
newNode.next = current.next;
27+
newNode.prev = current;
28+
current.next = newNode;
29+
}
30+
31+
remove(item) {
32+
let currNode = this.find(item);
33+
if(!(currNode.next === null)) {
34+
currNode.prev.next = currNode.next;
35+
currNode.next.prev = currNode.prev;
36+
currNode.next = null;
37+
currNode.prev = null;
38+
}
39+
}
40+
41+
findLast() {
42+
let currNode = this.head;
43+
while (!(currNode.next === null)) {
44+
currNode = currNode.next;
45+
}
46+
return currNode;
47+
}
48+
49+
display() {
50+
let currNode = this.head;
51+
while (!(currNode.next === null)) {
52+
console.log(currNode.next.element);
53+
currNode = currNode.next;
54+
}
55+
}
56+
57+
displayReverse() {
58+
let currNode = this.findLast();
59+
while (!(currNode.prev === null)) {
60+
console.log(currNode.element);
61+
currNode = currNode.prev;
62+
}
63+
}
64+
65+
advance(n) {
66+
let position = n;
67+
while(!(this.current.next === null)) {
68+
if(n-- > 0) {
69+
this.current = this.current.next;
70+
} else {
71+
console.log(`Can't advanced ${position} position in the list`);
72+
}
73+
}
74+
}
75+
76+
back(n) {
77+
let position = n;
78+
while(!(this.current.prev === null)) {
79+
if(n-- > 0) {
80+
this.current = this.current.prev;
81+
} else if (this.current.prev === null) {
82+
console.log(`Can't back ${position} position in the list`);
83+
break;
84+
} else {
85+
break;
86+
}
87+
}
88+
}
89+
90+
show() {
91+
console.log(this.current.element);
92+
}
93+
94+
}
95+
// Implementation
96+
// ##########################################
97+
/*
98+
1. Implement the advance(n) function so that when executed, the current node is moved n nodes forward in the list.
99+
*/
100+
console.log('CHAPTER 6');
101+
console.log('### Excercise 2');
102+
103+
const cities = new DLList();
104+
cities.insert("Conway", "Head");
105+
cities.insert("Russellville", "Conway");
106+
cities.insert("Carlisle", "Russellville");
107+
cities.insert("Alma", "Carlisle");
108+
console.log('DLList elements: ');
109+
cities.display();
110+
console.log('\nremoved Carlisle\n');
111+
cities.remove("Carlisle");
112+
console.log('\nDLList elements reversed: ');
113+
cities.displayReverse();
114+
115+
console.log('\nDouble Linked List Elements:');
116+
cities.display();
117+
118+
console.log('\nAdvance 3 positions');
119+
cities.advance(3);
120+
console.log('Display current Node');
121+
cities.show();
122+
123+
console.log('\nBack 2 positions');
124+
cities.back(2);
125+
console.log('Display current Node');
126+
cities.show();
127+
128+
console.log('\nBack 2 positions More');
129+
cities.back(2);
130+
console.log('Display current Node');
131+
cities.show();

06-chapter-3-Circular-Linked-List.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
class Node {
2+
constructor(element = 'Head', next = null) {
3+
this.element = element;
4+
this.next = next;
5+
}
6+
}
7+
8+
class LList {
9+
constructor(head = new Node()) {
10+
this.head = head;
11+
this.head.next = this.head;
12+
this.current = this.head;
13+
}
14+
15+
find(item) {
16+
let currNode = this.head;
17+
while (currNode.element != item) {
18+
currNode = currNode.next;
19+
}
20+
return currNode;
21+
}
22+
23+
findPrevious(item) {
24+
let currNode = this.head;
25+
while (!(currNode.next.element === 'Head') && currNode.next.element != item) {
26+
currNode = currNode.next;
27+
}
28+
return currNode;
29+
}
30+
31+
insert(newElement, item){
32+
const newNode = new Node(newElement);
33+
const current = this.find(item);
34+
newNode.next = current.next;
35+
current.next = newNode;
36+
}
37+
38+
remove(item) {
39+
let prevNode = this.findPrevious(item);
40+
prevNode.next = prevNode.next.next;
41+
}
42+
43+
display() {
44+
let currNode = this.head;
45+
while (currNode.next.element !== 'Head') {
46+
console.log(currNode.next.element);
47+
currNode = currNode.next;
48+
}
49+
}
50+
51+
advance(n) {
52+
let position = n;
53+
while(n-- > 0) {
54+
this.current = this.current.next;
55+
}
56+
}
57+
58+
back(n) {
59+
let position = n;
60+
while(n-- > 0) {
61+
this.current = this.current.prev;
62+
}
63+
}
64+
65+
show() {
66+
console.log(this.current.element);
67+
}
68+
69+
}
70+
71+
// Implementation
72+
// ##########################################
73+
/*
74+
1. Implement the advance(n) function so that when executed, the current node is moved n nodes forward in the list.
75+
*/
76+
console.log('CHAPTER 6');
77+
console.log('### Excercise 3');
78+
79+
const cities = new LList();
80+
cities.insert("Conway", "Head");
81+
cities.insert("Russellville", "Conway");
82+
cities.insert("Carlisle", "Russellville");
83+
cities.insert("Alma", "Carlisle");
84+
cities.display();
85+
console.log('remove; Carlisle\n');
86+
cities.remove("Carlisle");
87+
88+
console.log('Circular Linked List Elements:');
89+
cities.display();
90+
91+
console.log('\nAdvance 3 positions');
92+
cities.advance(3);
93+
console.log('Display current Node');
94+
cities.show();
95+
96+
console.log('\nAdvance 2 more positions');
97+
cities.advance(2);
98+
console.log('Display current Node');
99+
cities.show();

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
1111

1212
| **Num** | **Type** | **Exercises** | **Description** |
1313
--- | --- | --- | ---
14-
| 1.- | Array | 4 | The `Array` is the most common data structure in computer programming
15-
| 2.- | Lists | 5 | A `List` is an ordered sequence of data, where elements are not meant to be ordered.
16-
| 3.- | Stacks | 3 | A `Stack` is an example of Last-in, First-Out (LIFO)
17-
| 4.- | Queues | | A `Queue` is an example of First-in, First-Out (FIFO)
14+
| 1.- | [Array](./02-chapter-Arrays.js) | 4 | The `Array` is the most common data structure in computer programming
15+
| 2.- | [Lists](./03-chapter-List.js) | 5 | A `List` is an ordered sequence of data, where elements are not meant to be ordered.
16+
| 3.- | [Stacks](./04-chapter-Stacks.js) | 3 | A `Stack` is an example of Last-in, First-Out (LIFO)
17+
| 4.- | [Queues](./05-chapter-Queues.js) | 3 | A `Queue` is an example of First-in, First-Out (FIFO)
18+
| 5.- | [Linked List](./06-chapter-1-Linked-List.js) | 4 | A linked list is a collection of objects called nodes. Each node is linked to a successor node in the list using an object reference.
19+
| 6.- | [Double Linked List](./06-chapter-2-Double-Linked-List.js) | 4 | Traversing a Double linked list are more efficient, since we no longer have to search for the previous node.
20+
| 7.- | [Circular Linked List](./) | 4 | The reason you might want to create a circularly linked list is if you want the ability to go backward through a list but don’t want the extra overhead of creating a doubly linked list.

0 commit comments

Comments
 (0)