Skip to content

Commit 79a8886

Browse files
authored
fix: redo linkedlists, stacks and queues (careercup#61)
This commit fixes the implementations for stacks, linked lists (singly) and queues, across Chapter 2, 3, 4 and 7. Maintains the previous interface for b/c.
1 parent fbf50ff commit 79a8886

File tree

10 files changed

+425
-183
lines changed

10 files changed

+425
-183
lines changed

chapter02/util/LinkedListX.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class LinkedList {
6868
let first = this.head;
6969
if (this.head && this.head.next) {
7070
this.head = this.head.next;
71+
first.next = null;
7172
}
73+
else this.head = null;
7274
return first;
7375
}
7476

chapter03/util/LinkedList.js

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,84 @@
1-
var LinkedList = function(value) {
2-
this.value = value;
3-
this.next = null;
4-
};
1+
// moved from chapter 2 (/util/LinkedListX.js)
52

6-
module.exports = LinkedList;
3+
class Node {
4+
constructor(value) {
5+
this.value = value
6+
this.next = null;
7+
}
8+
}
9+
10+
class LinkedList {
11+
constructor() {
12+
this.head = null;
13+
this.tail = null;
14+
}
15+
16+
append(value) {
17+
let node = new Node(value);
18+
// if list is empty
19+
if (!this.head) {
20+
this.head = node;
21+
this.tail = node;
22+
}
23+
else {
24+
this.tail.next = node;
25+
this.tail = node;
26+
}
27+
}
28+
29+
prepend(value) {
30+
let node = new Node(value);
31+
node.next = this.head;
32+
this.head = node;
33+
}
34+
35+
pop() {
36+
let cur = this.head;
37+
38+
// only one or no item exists
39+
if (!cur) return null;
40+
if (!cur.next) {
41+
this.head = null;
42+
return cur;
43+
}
44+
// move till the 2nd last
45+
while (cur.next.next)
46+
cur = cur.next;
47+
48+
let last = this.tail;
49+
this.tail = cur;
50+
this.tail.next = null;
51+
return last;
52+
}
53+
54+
popFirst() {
55+
let first = this.head;
56+
if (this.head && this.head.next) {
57+
this.head = this.head.next;
58+
first.next = null;
59+
}
60+
else this.head = null;
61+
return first;
62+
}
63+
64+
head() {
65+
return this.head;
66+
}
67+
68+
tail() {
69+
return this.tail;
70+
}
71+
72+
_toArray() {
73+
let arr = [];
74+
let cur = this.head;
75+
while (cur) {
76+
arr.push(cur.value);
77+
cur = cur.next;
78+
}
79+
80+
return arr;
81+
}
82+
}
83+
84+
module.exports = LinkedList;

chapter03/util/Queue.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
// implement a queue using linkedLists
22
var LinkedList = require('./LinkedList');
33

4-
var Queue = function() {
5-
this.front = null;
6-
this.back = null;
7-
};
8-
9-
Queue.prototype.add = function(value) {
10-
var node = new LinkedList(value);
11-
if (this.front === null) {
12-
this.front = node;
13-
this.back = node;
14-
} else {
15-
var prevBack = this.back;
16-
this.back = node;
17-
prevBack.next = this.back;
4+
class Queue {
5+
constructor() {
6+
this._list = new LinkedList();
187
}
19-
};
20-
21-
Queue.prototype.remove = function() {
22-
var removed = this.front;
23-
if (this.front === this.back) {
24-
this.front = null;
25-
this.back = null;
26-
} else {
27-
this.front = this.front.next;
8+
9+
enqueue(value) {
10+
this._list.append(value);
11+
}
12+
13+
dequeue() {
14+
let node = this._list.popFirst();
15+
return node.value;
16+
}
17+
18+
peek() {
19+
return this._list.head ? this._list.head.value : null;
20+
}
21+
22+
isEmpty() {
23+
return this._list.head == null;
2824
}
29-
return removed !== null ? removed.value : null;
30-
};
3125

32-
Queue.prototype.peek = function() {
33-
return this.front !== null ? this.front.value : null;
34-
};
26+
_toArray() {
27+
return this._list._toArray();
28+
}
29+
}
3530

36-
Queue.prototype.isEmpty = function() {
37-
return this.front === null;
38-
};
31+
// alias
32+
Queue.prototype.add = Queue.prototype.enqueue;
33+
Queue.prototype.remove = Queue.prototype.dequeue;
3934

4035
module.exports = Queue;
4136

@@ -44,8 +39,10 @@ module.exports = Queue;
4439
// q.add('a');
4540
// q.add('b');
4641
// q.add('c');
42+
// console.log(q._toArray());
4743
// console.log(q.remove(), 'a');
4844
// console.log(q.peek(), 'b');
4945
// console.log(q.remove(), 'b');
5046
// console.log(q.remove(), 'c');
5147
// console.log(q.isEmpty(), true);
48+
// console.log(q._toArray());

chapter03/util/Stack.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1-
// implement a stack using linkedLists
2-
var LinkedList = require('./LinkedList');
3-
4-
var Stack = function() {
5-
this.top = null;
6-
};
7-
8-
Stack.prototype.push = function(value) {
9-
var node = new LinkedList(value);
10-
node.next = this.top;
11-
this.top = node;
12-
};
13-
14-
Stack.prototype.pop = function() {
15-
var popped = this.top;
16-
if (this.top !== null) {
17-
this.top = this.top.next;
1+
// simple implementation of a stack
2+
// using an Array
3+
4+
/*
5+
Interface:
6+
- push(value)
7+
- pop()
8+
- peek()
9+
- isEmpty()
10+
- size()
11+
*/
12+
class Stack {
13+
constructor() {
14+
this._data = [];
1815
}
19-
return popped.value;
20-
};
2116

22-
Stack.prototype.peek = function() {
23-
return this.top !== null ? this.top.value : null;
24-
};
17+
size() {
18+
return this._data.length;
19+
}
20+
21+
isEmpty() {
22+
return this.size() == 0;
23+
}
24+
25+
push(value) {
26+
this._data.push(value);
27+
}
28+
29+
pop() {
30+
return this._data.pop();
31+
}
2532

26-
Stack.prototype.isEmpty = function() {
27-
return this.top === null;
28-
};
33+
peek() {
34+
if (this.isEmpty()) return null;
35+
return this._data[this.size() - 1];
36+
}
37+
}
2938

3039
module.exports = Stack;
3140

@@ -39,4 +48,4 @@ module.exports = Stack;
3948
// console.log(s.peek(), 'b');
4049
// console.log(s.pop(), 'b');
4150
// console.log(s.pop(), 'a');
42-
// console.log(s.isEmpty(), true);
51+
// console.log(s.isEmpty(), true);

chapter04/util/LinkedList.js

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,84 @@
1-
var LinkedList = function(value) {
2-
this.value = value;
3-
this.next = null;
4-
};
1+
// moved from chapter 2 (/util/LinkedListX.js)
52

6-
module.exports = LinkedList;
3+
class Node {
4+
constructor(value) {
5+
this.value = value
6+
this.next = null;
7+
}
8+
}
9+
10+
class LinkedList {
11+
constructor() {
12+
this.head = null;
13+
this.tail = null;
14+
}
15+
16+
append(value) {
17+
let node = new Node(value);
18+
// if list is empty
19+
if (!this.head) {
20+
this.head = node;
21+
this.tail = node;
22+
}
23+
else {
24+
this.tail.next = node;
25+
this.tail = node;
26+
}
27+
}
28+
29+
prepend(value) {
30+
let node = new Node(value);
31+
node.next = this.head;
32+
this.head = node;
33+
}
34+
35+
pop() {
36+
let cur = this.head;
37+
38+
// only one or no item exists
39+
if (!cur) return null;
40+
if (!cur.next) {
41+
this.head = null;
42+
return cur;
43+
}
44+
// move till the 2nd last
45+
while (cur.next.next)
46+
cur = cur.next;
47+
48+
let last = this.tail;
49+
this.tail = cur;
50+
this.tail.next = null;
51+
return last;
52+
}
53+
54+
popFirst() {
55+
let first = this.head;
56+
if (this.head && this.head.next) {
57+
this.head = this.head.next;
58+
first.next = null;
59+
}
60+
else this.head = null;
61+
return first;
62+
}
63+
64+
head() {
65+
return this.head;
66+
}
67+
68+
tail() {
69+
return this.tail;
70+
}
71+
72+
_toArray() {
73+
let arr = [];
74+
let cur = this.head;
75+
while (cur) {
76+
arr.push(cur.value);
77+
cur = cur.next;
78+
}
79+
80+
return arr;
81+
}
82+
}
83+
84+
module.exports = LinkedList;

0 commit comments

Comments
 (0)