Skip to content

Commit 4e95061

Browse files
committedJul 9, 2014
added chapter 05
1 parent 5cc6aa1 commit 4e95061

8 files changed

+481
-0
lines changed
 

‎chapter05/01-Linked-List.js

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
function LinkedList() {
2+
3+
var Node = function(element){
4+
5+
this.element = element;
6+
this.next = null;
7+
};
8+
9+
var length = 0;
10+
var head = null;
11+
12+
this.append = function(element){
13+
14+
var node = new Node(element),
15+
current;
16+
17+
if (head === null){ //first node on list
18+
head = node;
19+
} else {
20+
21+
current = head;
22+
23+
//loop the list until find last item
24+
while(current.next){
25+
current = current.next;
26+
}
27+
28+
//get last item and assign next to added item to make the link
29+
current.next = node;
30+
}
31+
32+
length++; //update size of list
33+
};
34+
35+
this.insert = function(position, element){
36+
37+
//check for out-of-bounds values
38+
if (position >= 0 && position <= length){
39+
40+
var node = new Node(element),
41+
current = head,
42+
previous,
43+
index = 0;
44+
45+
if (position === 0){ //add on first position
46+
47+
node.next = current;
48+
head = node;
49+
50+
} else {
51+
while (index++ < position){
52+
previous = current;
53+
current = current.next;
54+
}
55+
node.next = current;
56+
previous.next = node;
57+
}
58+
59+
length++; //update size of list
60+
61+
return true;
62+
63+
} else {
64+
return false;
65+
}
66+
};
67+
68+
this.removeAt = function(position){
69+
70+
//check for out-of-bounds values
71+
if (position > -1 && position < length){
72+
73+
var current = head,
74+
previous,
75+
index = 0;
76+
77+
//removing first item
78+
if (position === 0){
79+
head = current.next;
80+
} else {
81+
82+
while (index++ < position){
83+
84+
previous = current;
85+
current = current.next;
86+
}
87+
88+
//link previous with current's next - skip it to remove
89+
previous.next = current.next;
90+
}
91+
92+
length--;
93+
94+
return current.element;
95+
96+
} else {
97+
return null;
98+
}
99+
};
100+
101+
this.remove = function(element){
102+
103+
var index = this.indexOf(element);
104+
return this.removeAt(index);
105+
};
106+
107+
this.indexOf = function(element){
108+
109+
var current = head,
110+
index = -1;
111+
112+
//check first item
113+
if (element == current.element){
114+
return 0;
115+
}
116+
117+
index++;
118+
119+
//check in the middle of the list
120+
while(current.next){
121+
122+
if (element == current.element){
123+
return index;
124+
}
125+
126+
current = current.next;
127+
index++;
128+
}
129+
130+
//check last item
131+
if (element == current.element){
132+
return index;
133+
}
134+
135+
return -1;
136+
};
137+
138+
this.isEmpty = function() {
139+
return length === 0;
140+
};
141+
142+
this.size = function() {
143+
return length;
144+
};
145+
146+
this.getHead = function(){
147+
return head;
148+
};
149+
150+
this.toString = function(){
151+
152+
var current = head,
153+
s = current.element;
154+
155+
while(current.next){
156+
current = current.next;
157+
s += ', ' + current.element;
158+
}
159+
160+
return s;
161+
};
162+
163+
this.print = function(){
164+
console.log(this.toString());
165+
}
166+
}

‎chapter05/02-UsingLinkedLists.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Linked-List.js"></script>
9+
<script type="text/javascript" src="02-UsingLinkedLists.js"></script>
10+
</body>
11+
</html>

‎chapter05/02-UsingLinkedLists.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var list = new LinkedList();
2+
list.append(15);
3+
list.print();
4+
console.log(list.indexOf(15));
5+
list.append(10);
6+
list.print();
7+
console.log(list.indexOf(10));
8+
list.append(13);
9+
list.print();
10+
console.log(list.indexOf(13));
11+
console.log(list.indexOf(10));
12+
list.append(11);
13+
list.append(12);
14+
list.print();
15+
console.log(list.removeAt(1));
16+
list.print()
17+
console.log(list.removeAt(3));
18+
list.print();
19+
list.append(14);
20+
list.print();
21+
list.insert(0,16);
22+
list.print();
23+
list.insert(1,17);
24+
list.print();
25+
list.insert(list.size(),18);
26+
list.print();
27+
list.remove(16);
28+
list.print();
29+
list.remove(11);
30+
list.print();
31+
list.remove(18);
32+
list.print();

‎chapter05/03-Doubly-Linked-List.js

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
function DoublyLinkedList() {
2+
3+
var Node = function(element){
4+
5+
this.element = element;
6+
this.next = null;
7+
this.prev = null; //NEW
8+
};
9+
10+
var length = 0;
11+
var head = null;
12+
var tail = null; //NEW
13+
14+
this.append = function(element){
15+
16+
var node = new Node(element),
17+
current;
18+
19+
if (head === null){ //first node on list
20+
head = node;
21+
tail = node; //NEW
22+
} else {
23+
24+
//attach to the tail node //NEW
25+
tail.next = node;
26+
node.prev = tail;
27+
tail = node;
28+
}
29+
30+
length++; //update size of list
31+
};
32+
33+
this.insert = function(position, element){
34+
35+
//check for out-of-bounds values
36+
if (position >= 0 && position <= length){
37+
38+
var node = new Node(element),
39+
current = head,
40+
previous,
41+
index = 0;
42+
43+
if (position === 0){ //add on first position
44+
45+
node.next = current;
46+
current.prev = node; //NEW {1}
47+
head = node;
48+
49+
} else if (position === length-1) { //last item //NEW
50+
51+
current = tail; // {2}
52+
current.next = node;
53+
node.prev = current;
54+
tail = node;
55+
56+
} else {
57+
while (index++ < position){ //{3}
58+
previous = current;
59+
current = current.next;
60+
}
61+
node.next = current;
62+
previous.next = node;
63+
64+
current.prev = node; //NEW
65+
node.prev = previous; //NEW
66+
}
67+
68+
length++; //update size of list
69+
70+
return true;
71+
72+
} else {
73+
return false;
74+
}
75+
};
76+
77+
this.removeAt = function(position){
78+
79+
//check for out-of-bounds values
80+
if (position > -1 && position < length){
81+
82+
var current = head,
83+
previous,
84+
index = 0;
85+
86+
//removing first item
87+
if (position === 0){
88+
89+
head = current.next; // {1}
90+
91+
//if there is only one item, then we update tail as well //NEW
92+
if (length === 1){ // {2}
93+
tail = null;
94+
} else {
95+
head.prev = null; // {3}
96+
}
97+
98+
} else if (position === length-1){ //last item //NEW
99+
100+
current = tail; // {4}
101+
tail = current.prev;
102+
tail.next = null;
103+
104+
} else {
105+
106+
while (index++ < position){ // {5}
107+
108+
previous = current;
109+
current = current.next;
110+
}
111+
112+
//link previous with current's next - skip it to remove
113+
previous.next = current.next; // {6}
114+
current.next.prev = previous; //NEW
115+
}
116+
117+
length--;
118+
119+
return current.element;
120+
121+
} else {
122+
return null;
123+
}
124+
};
125+
126+
this.remove = function(element){
127+
128+
var index = this.indexOf(element);
129+
return this.removeAt(index);
130+
};
131+
132+
this.indexOf = function(element){
133+
134+
var current = head,
135+
index = -1;
136+
137+
//check first item
138+
if (element == current.element){
139+
return 0;
140+
}
141+
142+
index++;
143+
144+
//check in the middle of the list
145+
while(current.next){
146+
147+
if (element == current.element){
148+
return index;
149+
}
150+
151+
current = current.next;
152+
index++;
153+
}
154+
155+
//check last item
156+
if (element == current.element){
157+
return index;
158+
}
159+
160+
return -1;
161+
};
162+
163+
this.isEmpty = function() {
164+
return length === 0;
165+
};
166+
167+
this. size = function() {
168+
return length;
169+
};
170+
171+
this.toString = function(){
172+
173+
var current = head,
174+
s = current ? current.element : '';
175+
176+
while(current && current.next){
177+
current = current.next;
178+
s += ', ' + current.element;
179+
}
180+
181+
return s;
182+
};
183+
184+
this.inverseToString = function() {
185+
186+
var current = tail,
187+
s = current ? current.element : '';
188+
189+
while(current && current.prev){
190+
current = current.prev;
191+
s += ', ' + current.element;
192+
}
193+
194+
return s;
195+
};
196+
197+
this.print = function(){
198+
console.log(this.toString());
199+
};
200+
201+
this.printInverse = function(){
202+
console.log(this.inverseToString());
203+
};
204+
205+
this.getHead = function(){
206+
return head;
207+
};
208+
209+
this.getTail = function(){
210+
return tail;
211+
}
212+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="03-Doubly-Linked-List.js"></script>
9+
<script type="text/javascript" src="04-UsingDoublyLinkedLists.js"></script>
10+
</body>
11+
</html>
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var list = new DoublyLinkedList();
2+
3+
list.append(15);
4+
list.print();
5+
list.printInverse();
6+
7+
list.append(16);
8+
list.print();
9+
list.printInverse();
10+
11+
list.append(17);
12+
list.print();
13+
list.printInverse();
14+
15+
list.insert(0,13);
16+
list.print();
17+
list.printInverse();
18+
19+
list.insert(list.size()-1,18);
20+
list.print();
21+
list.printInverse();
22+
23+
list.insert(1,14);
24+
list.print();
25+
list.printInverse();
26+
27+
list.removeAt(0);
28+
list.print();
29+
list.printInverse();
30+
31+
list.removeAt(list.size()-1);
32+
list.print();
33+
list.printInverse();
34+
35+
list.removeAt(1);
36+
list.print();
37+
list.printInverse();
38+
39+
list.remove(16);
40+
list.print();
41+
list.printInverse();
42+
43+
list.remove(14);
44+
list.print();
45+
list.printInverse();
46+
47+
list.remove(17);
48+
list.print();
49+
list.printInverse();

‎chapter05/05-CircularLinkedList.js

Whitespace-only changes.

‎chapter05/06-CircularDoublyLinkedList.js

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.