Skip to content

Commit d69668f

Browse files
committed
added CircularLinkedList class in ES6 syntax
1 parent f6332b4 commit d69668f

3 files changed

+207
-5
lines changed

chapter05/05-CircularLinkedList2.js

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

chapter05/06-UsingCircularLinkedList.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="05-CircularLinkedList.js"></script>
9-
<script type="text/javascript" src="06-UsingCircularLinkedList.js"></script>
8+
<script src="05-CircularLinkedList.js"></script>
9+
<script src="05-CircularLinkedList2.js"></script>
10+
<script src="06-UsingCircularLinkedList.js"></script>
1011
</body>
1112
</html>

chapter05/06-UsingCircularLinkedList.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
let circularLinkedList = new CircularLinkedList();
1+
let circularLinkedList = new CircularLinkedList2();
22

3-
circularLinkedList.append(15);
3+
/*circularLinkedList.append(15);
44
circularLinkedList.print();
55
66
circularLinkedList.append(16);
@@ -25,4 +25,4 @@ circularLinkedList.removeAt(2);
2525
circularLinkedList.print();
2626
2727
console.log(circularLinkedList.indexOf(14.5));
28-
console.log(circularLinkedList.indexOf(16));
28+
console.log(circularLinkedList.indexOf(16));*/

0 commit comments

Comments
 (0)