Skip to content

Commit 4c18af4

Browse files
committed
added LinkedList class in ES6 syntax
1 parent 7641833 commit 4c18af4

4 files changed

+46
-26
lines changed

chapter05/01-LinkedList.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function LinkedList() {
138138
string = '';
139139

140140
while (current) {
141-
string += current.element + (current.next ? '\n' : '');
141+
string += current.element + (current.next ? ', ' : '');
142142
current = current.next;
143143
}
144144
return string;

chapter05/01-LinkedList2.js

+42-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
let LinkedList2 = (function () {
22

33
class Node {
4-
constructor(element, priority){
4+
constructor(element){
55
this.element = element;
66
this.next = null;
77
}
88
}
99

10-
let length = 0;
11-
let head = null;
10+
const length = new WeakMap();
11+
const head = new WeakMap();
1212

1313
class LinkedList2 {
1414

15+
constructor () {
16+
length.set(this, 0);
17+
head.set(this, null);
18+
}
19+
1520
append(element) {
1621

1722
let node = new Node(element),
18-
current;
23+
current,
24+
_head = this.getHead();
1925

20-
if (head === null) { //first node on list
21-
head = node;
26+
if (_head === null) { //first node on list
27+
_head = node;
28+
head.set(this, _head);
2229
} else {
2330

24-
current = head;
31+
current = _head;
2532

2633
//loop the list until find last item
2734
while (current.next) {
@@ -32,23 +39,28 @@ let LinkedList2 = (function () {
3239
current.next = node;
3340
}
3441

35-
length++; //update size of list
42+
//update size of list
43+
let l = this.size();
44+
l++;
45+
length.set(this, l);
3646
}
3747

3848
insert(position, element) {
3949

4050
//check for out-of-bounds values
41-
if (position >= 0 && position <= length) {
51+
if (position >= 0 && position <= this.size()) {
4252

4353
let node = new Node(element),
44-
current = head,
54+
_head = this.getHead(),
55+
current = _head,
4556
previous,
4657
index = 0;
4758

4859
if (position === 0) { //add on first position
4960

5061
node.next = current;
51-
head = node;
62+
_head = node;
63+
head.set(this, _head);
5264

5365
} else {
5466
while (index++ < position) {
@@ -59,7 +71,10 @@ let LinkedList2 = (function () {
5971
previous.next = node;
6072
}
6173

62-
length++; //update size of list
74+
//update size of list
75+
let l = this.size();
76+
l++;
77+
length.set(this, l);
6378

6479
return true;
6580

@@ -71,15 +86,17 @@ let LinkedList2 = (function () {
7186
removeAt(position) {
7287

7388
//check for out-of-bounds values
74-
if (position > -1 && position < length) {
89+
if (position > -1 && position < this.size()) {
7590

76-
let current = head,
91+
let _head = this.getHead(),
92+
current = _head,
7793
previous,
7894
index = 0;
7995

8096
//removing first item
8197
if (position === 0) {
82-
head = current.next;
98+
_head = current.next;
99+
head.set(this, _head);
83100
} else {
84101

85102
while (index++ < position) {
@@ -92,7 +109,9 @@ let LinkedList2 = (function () {
92109
previous.next = current.next;
93110
}
94111

95-
length--;
112+
let l = this.size();
113+
l--;
114+
length.set(this, l);
96115

97116
return current.element;
98117

@@ -109,7 +128,8 @@ let LinkedList2 = (function () {
109128

110129
indexOf(element) {
111130

112-
let current = head,
131+
let _head = this.getHead(),
132+
current = _head,
113133
index = 0;
114134

115135
while (current) {
@@ -124,24 +144,24 @@ let LinkedList2 = (function () {
124144
}
125145

126146
isEmpty() {
127-
return length === 0;
147+
return this.size() === 0;
128148
}
129149

130150
size() {
131-
return length;
151+
return length.get(this);
132152
}
133153

134154
getHead() {
135-
return head;
155+
return head.get(this);
136156
}
137157

138158
toString() {
139159

140-
let current = head,
160+
let current = this.getHead(),
141161
string = '';
142162

143163
while (current) {
144-
string += current.element + (current.next ? '\n' : '');
164+
string += current.element + (current.next ? ', ' : '');
145165
current = current.next;
146166
}
147167
return string;

chapter05/02-UsingLinkedLists.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="01-LinkedList.js"></script>
8+
<script src="01-LinkedList.js"></script>
99
<script src="01-LinkedList2.js"></script>
10-
<script type="text/javascript" src="02-UsingLinkedLists.js"></script>
10+
<script src="02-UsingLinkedLists.js"></script>
1111
</body>
1212
</html>

chapter05/02-UsingLinkedLists.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let list = new LinkedList();
1+
let list = new LinkedList2();
22
list.append(15);
33
list.print();
44
console.log(list.indexOf(15));

0 commit comments

Comments
 (0)