Skip to content

Commit 806f16a

Browse files
committedOct 22, 2017
chapter 05: [LinkedLists]
1 parent 3dcc88e commit 806f16a

File tree

6 files changed

+20
-37
lines changed

6 files changed

+20
-37
lines changed
 

‎examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/js/data-structures/circular-linked-list.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ export default class CircularLinkedList extends LinkedList {
66
constructor(equalsFn = defaultEquals) {
77
super(equalsFn);
88
}
9-
getLastElement() {
10-
let current = this.head;
11-
for (let i = 2; i <= this.size() && current != null; i++) {
12-
current = current.next;
13-
}
14-
return current;
15-
}
169
push(element) {
1710
const node = new Node(element);
1811
let current;
1912
if (this.head == null) {
2013
this.head = node;
2114
} else {
22-
current = this.getLastElement();
15+
current = this.getElementAt(this.size() - 1);
2316
current.next = node;
2417
}
2518
// set node.next to head - to have circular list
@@ -37,7 +30,7 @@ export default class CircularLinkedList extends LinkedList {
3730
node.next = this.head;
3831
} else {
3932
node.next = current;
40-
current = this.getLastElement();
33+
current = this.getElementAt(this.size());
4134
// update last element
4235
this.head = node;
4336
current.next = this.head;
@@ -56,11 +49,11 @@ export default class CircularLinkedList extends LinkedList {
5649
if (index >= 0 && index < this.count) {
5750
let current = this.head;
5851
if (index === 0) {
59-
const removed = this.head;
6052
if (this.size() === 1) {
6153
this.head = undefined;
6254
} else {
63-
current = this.getLastElement();
55+
const removed = this.head;
56+
current = this.getElementAt(this.size() - 1);
6457
this.head = this.head.next;
6558
current.next = this.head;
6659
current = removed;

‎src/js/data-structures/doubly-linked-list.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DoublyNode } from './models/linked-list-models';
55
export default class DoublyLinkedList extends LinkedList {
66
constructor(equalsFn = defaultEquals) {
77
super(equalsFn);
8+
this.tail = undefined;
89
}
910
push(element) {
1011
const node = new DoublyNode(element);
@@ -24,18 +25,16 @@ export default class DoublyLinkedList extends LinkedList {
2425
const node = new DoublyNode(element);
2526
let current = this.head;
2627
if (index === 0) {
27-
if (this.head == null) {
28-
// NEW
28+
if (this.head == null) { // NEW
2929
this.head = node;
30-
this.tail = node;
30+
this.tail = node; // NEW
3131
} else {
3232
node.next = this.head;
3333
this.head.prev = node; // NEW
3434
this.head = node;
3535
}
36-
} else if (index === this.count) {
37-
// last item // NEW
38-
current = this.tail; // {2}
36+
} else if (index === this.count) { // last item NEW
37+
current = this.tail;
3938
current.next = node;
4039
node.prev = current;
4140
this.tail = node;
@@ -56,24 +55,24 @@ export default class DoublyLinkedList extends LinkedList {
5655
if (index >= 0 && index < this.count) {
5756
let current = this.head;
5857
if (index === 0) {
59-
this.head = this.head.next; // {1}
58+
this.head = this.head.next;
6059
// if there is only one item, then we update tail as well //NEW
6160
if (this.count === 1) {
6261
// {2}
6362
this.tail = undefined;
6463
} else {
65-
this.head.prev = undefined; // {3}
64+
this.head.prev = undefined;
6665
}
6766
} else if (index === this.count - 1) {
6867
// last item //NEW
69-
current = this.tail; // {4}
68+
current = this.tail;
7069
this.tail = current.prev;
7170
this.tail.next = undefined;
7271
} else {
7372
current = this.getElementAt(index);
7473
const previous = current.prev;
7574
// link previous with current's next - skip it to remove
76-
previous.next = current.next; // {6}
75+
previous.next = current.next;
7776
current.next.prev = previous; // NEW
7877
}
7978
this.count--;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export default class LinkedList {
3535
insert(element, index) {
3636
if (index >= 0 && index <= this.count) {
3737
const node = new Node(element);
38-
const current = this.head;
3938
if (index === 0) {
39+
const current = this.head;
4040
node.next = current;
4141
this.head = node;
4242
} else {

‎src/ts/data-structures/circular-linked-list.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,14 @@ export default class CircularLinkedList<T> extends LinkedList<T> {
77
super(equalsFn);
88
}
99

10-
private getLastElement() {
11-
let current = this.head;
12-
for (let i = 2; i <= this.size() && current != null; i++) {
13-
current = current.next;
14-
}
15-
return current;
16-
}
17-
1810
push(element: T) {
1911
const node = new Node(element);
2012
let current;
2113

2214
if (this.head == null) {
2315
this.head = node;
2416
} else {
25-
current = this.getLastElement();
17+
current = this.getElementAt(this.size() - 1);
2618
current.next = node;
2719
}
2820

@@ -44,7 +36,7 @@ export default class CircularLinkedList<T> extends LinkedList<T> {
4436
node.next = this.head;
4537
} else {
4638
node.next = current;
47-
current = this.getLastElement();
39+
current = this.getElementAt(this.size());
4840
// update last element
4941
this.head = node;
5042
current.next = this.head;
@@ -65,12 +57,11 @@ export default class CircularLinkedList<T> extends LinkedList<T> {
6557
let current = this.head;
6658

6759
if (index === 0) {
68-
const removed = this.head;
69-
7060
if (this.size() === 1) {
7161
this.head = undefined;
7262
} else {
73-
current = this.getLastElement();
63+
const removed = this.head;
64+
current = this.getElementAt(this.size() - 1);
7465
this.head = this.head.next;
7566
current.next = this.head;
7667
current = removed;

‎src/ts/data-structures/linked-list.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export default class LinkedList<T> {
4040
insert(element: T, index: number) {
4141
if (index >= 0 && index <= this.count) {
4242
const node = new Node(element);
43-
const current = this.head;
4443

4544
if (index === 0) {
45+
const current = this.head;
4646
node.next = current;
4747
this.head = node;
4848
} else {

0 commit comments

Comments
 (0)