-
Notifications
You must be signed in to change notification settings - Fork 2
/
LinkedList.js
203 lines (163 loc) · 5.14 KB
/
LinkedList.js
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
196
197
198
199
200
201
202
203
'use strict'
const should = require('should');
const LinkedList = require('../LinkedList.js');
let ll = null
, ts = function(){return this.name;}
, numElements = [24, 34, 21, 35]
, strElements = ["mark", "bbc", "john", "lucas"]
, objElements = strElements.map( (str, i)=>{ return {name: str, age: i}});
function initLL(ll, type){
let eles = null;
switch(type){
case "string":
eles = strElements;
break;
case "number":
eles = numElements
break;
case "object":
eles = objElements
break;
default:
eles = numElements
break;
}
eles.forEach( e => ll.insertStart(e) );
}
describe("Linked List", function(){
beforeEach(() => {
ll = new LinkedList();
});
afterEach(() => {
ll.clear();
});
it('should initialized with no nodes', function(){
ll.length.should.be.equal(0);
should.not.exist(ll.head);
});
it('should add element at the end of the list when insertEnd() is called', function(){
for(let i = 0; i < strElements.length; i++){
let ele = strElements[i];
ll.insertEnd(ele);
ll.length.should.be.equal(i+1);
let cur = ll.head;
while(cur.next)
cur = cur.next;
cur.val.should.be.equal(ele);
}
});
it('should add element at the beginning of the list when insertStart() is called', function(){
for(let i = 0; i < strElements.length; i++){
let ele = strElements[i];
ll.insertStart(ele);
ll.length.should.be.equal(i+1);
ll.head.val.should.be.equal(ele);
}
});
it('should delete and return node at the beginning of the list when deleteFirst() is called', function(){
let arr = numElements;
for(let ele of arr){
ll.insertStart(ele);
}
for(let i=0; i<arr.length; i++){
let n = ll.deleteFirst();
n.val.should.be.equal(arr[arr.length -1 - i]);
should.not.exist(n.next);
if(i < arr.length - 1){
ll.head.val.should.be.equal( arr[arr.length - 2 - i] );
}
ll.length.should.be.equal(arr.length - 1 - i);
}
});
it('should delete and return node at the end of the list when deleteLast() is called ', function(){
let arr = ["mike", "bbc", "john", "lucas"], idx = arr.length - 1;
for(let ele of arr){
ll.insertStart(ele);
}
for(let i=0; i<arr.length; i++){
ll.deleteLast().val.should.be.equal(arr[i]);
if(i < arr.length - 1){
let cur = ll.head;
while(cur.next) // the next pointer of previous node should be null
cur=cur.next;
cur.val.should.be.equal(arr[i+1]);
}
ll.length.should.be.eql(idx -i);
}
});
it('should add node at the end of the list when insertEndNode() is called', function(){
initLL(ll);
let nodes = [];
while(ll.length)
nodes.push(ll.deleteFirst());
for(let i=0; i < nodes.length; i++){
ll.insertEndNode(nodes[i]);
let cur = ll.head;
while(cur.next)
cur = cur.next;
cur.should.be.equal(nodes[i]);
ll.length.should.be.equal(i + 1);
}
});
it('should add node at the beginning of the list when insertStartNode() is called', function(){
initLL(ll);
let nodes = [];
while(ll.length)
nodes.push(ll.deleteFirst());
for(let i=0; i < nodes.length; i++){
ll.insertStartNode(nodes[i]);
ll.head.should.be.equal(nodes[i]);
ll.length.should.be.equal(i + 1);
}
});
it('should delete all nodes when clear() is called', function(){
initLL(ll, "string");
ll.length.should.be.equal(strElements.length);
ll.clear();
ll.length.should.be.equal(0);
should.not.exist(ll.head);
});
it('should throw "InvalidOperation" error when deleteFirst()/deleteLast() is called upon an empty list', function(){
ll.deleteLast.bind(ll).should.throw(Error, {message: "InvalidOperation, the list is empty."});
ll.deleteFirst.bind(ll).should.throw(Error, {message: "InvalidOperation, the list is empty."});
});
it('should return array of return value of fn(element) according to the enqueue order when traverse() is called', function(){
let eles = [{name:"a", toString:ts}, {name:"b", toString:ts}, {name:"c", toString: ts}, {name:"d", toString: ts}];
for(let ele of eles)
ll.insertEnd(ele);
ll.traverse(e => e.name).should.be.eql(eles.map(e => e.name));// should deep equal because it is an array
});
it('should get string with "<->" between elements when toString() is called', function(){
let eles = ["m", "n", "o", "p"];
for(let ele of eles)
ll.insertEnd(ele);
ll.toString().should.be.eql(eles.join(" -> "));// should deep equal because it is an array
});
it('should return itself to support calling chain when insertStart[Node]()/insertEnd[Node]() is called', function(){
strElements.forEach(ele => {
ll.insertEnd(ele).should.be.equal(ll);
let n = ll.deleteLast();
ll.insertEndNode(n).should.be.equal(ll);
ll.clear();
ll.insertStart(ele).should.be.equal(ll);
n = ll.deleteFirst();
ll.insertStartNode(n).should.be.equal(ll);
});
});
it('should return itself to support calling chain when insertStart[Node]()/insertEnd[Node]() is called', function(){
var total = 1000;
for(var i=0;i<total;i++){
var step = Math.floor(Math.random() * 100);
for(var j=0;j<step;j++){
ll.insertStart(1);
}
step = Math.floor(Math.random() * 5);
for(j=0;j<step;j++){
ll.deleteFirst();
}
}
while(ll.length){
ll.deleteFirst();
}
});
});