-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list.js
More file actions
195 lines (147 loc) · 3.86 KB
/
linked-list.js
File metadata and controls
195 lines (147 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
const mocha = require('mocha');
const { assert } = require('chai');
function MyNode() {
this.setup = function(next, prev, val) {
this.next = next;
this.prev = prev;
this.val = val;
};
};
function MyLinkedList() {
this.setup = function() {
this.head = null;
this.tail = null;
};
this.extendHead = function(val) {
const node = new MyNode();
node.setup(null, this.head, val);
if (this.head)
this.head.next = node;
else
this.tail = node;
this.head = node;
};
this.removeHead = function() {
if (!this.head)
return null;
const val = this.head.val;
this.head = this.head.prev;
if (this.head)
this.head.next = null;
else
this.tail = null;
return val;
};
this.extendToTail = function(val) {
const node = new MyNode();
node.setup(this.tail, null, val);
if (this.tail)
this.tail.prev = node;
else
this.head = node;
this.tail = node;
};
this.removeTail = function() {
if (!this.tail)
return null;
const val = this.tail.val;
this.tail = this.tail.next;
if (this.tail)
this.tail.prev = null;
else
this.head = null;
return val;
};
this.search = function(val) {
let current = this.head;
while (current) {
if (current.val === val)
return val;
current = current.prev;
}
return null;
};
this.indexOf = function(val) {
let index = 0;
let current = this.tail;
const indexes = [];
while (current) {
if (current.val === val)
indexes.push(index);
current = current.next;
index++;
}
return indexes;
};
this.pingHead = function() {
console.log(this.head);
};
this.pingTail = function() {
console.log(this.tail);
};
this.getHeadValue = function() {
console.log('Head Valus:', this.head ? this.head.val: null);
};
this.getTailValue = function() {
console.log('Tail Valus:', this.tail ? this.tail.val: null);
};
};
describe("Cool Linked List", () => {
it("Add to head", () => {
const list = new MyLinkedList();
list.setup();
list.extendHead('a');
list.extendHead('b');
list.extendHead('c');
// c - b - a
assert.equal(list.tail.prev, null);
assert.equal(list.tail.val, 'a');
assert.equal(list.tail.next.val, 'b');
assert.equal(list.head.val, 'c');
assert.equal(list.head.prev.val, 'b');
assert.equal(list.head.next, null);
assert.equal(list.tail.next.next.val, 'c');
assert.equal(list.head.prev.prev.val, 'a');
});
it("Add to head - 2", () => {
const list = new MyLinkedList();
list.setup();
list.extendHead('a');
list.extendToTail('c');
list.extendHead('b');
// b - a - c
assert.equal(list.tail.prev, null);
assert.equal(list.tail.val, 'c');
assert.equal(list.tail.next.val, 'a');
assert.equal(list.head.val, 'b');
assert.equal(list.head.prev.val, 'a');
assert.equal(list.head.next, null);
assert.equal(list.tail.next.next.val, 'b');
assert.equal(list.head.prev.prev.val, 'c');
});
it("Search for val", () => {
const list = new MyLinkedList();
list.setup();
list.extendHead('a');
list.extendHead('b');
list.extendHead('c');
assert.equal(list.search('a'), 'a');
assert.equal(list.search('b'), 'b');
assert.equal(list.search('c'), 'c');
assert.equal(list.search('x'), null);
});
it("Search for indexes of vals", () => {
const list = new MyLinkedList();
list.setup();
list.extendToTail('a');
list.extendToTail('b');
list.extendToTail('c');
list.extendToTail('c');
list.extendToTail('a');
assert.deepEqual(list.indexOf('a'), [0, 4]);
assert.deepEqual(list.indexOf('b'), [3]);
assert.deepEqual(list.indexOf('c'), [1, 2]);
assert.deepEqual(list.indexOf('x'), []);
});
});