Skip to content

Commit 4e95061

Browse files
committed
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();

0 commit comments

Comments
 (0)