Skip to content

Commit ad6b7eb

Browse files
committed
updated code chapter 5 - linked lists
1 parent ed9abfa commit ad6b7eb

8 files changed

+35
-34
lines changed

chapter05/01-Linked-List.js chapter05/01-LinkedList.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
function LinkedList() {
22

3-
var Node = function(element){
3+
let Node = function(element){
44

55
this.element = element;
66
this.next = null;
77
};
88

9-
var length = 0;
10-
var head = null;
9+
let length = 0;
10+
let head = null;
1111

1212
this.append = function(element){
1313

14-
var node = new Node(element),
14+
let node = new Node(element),
1515
current;
1616

1717
if (head === null){ //first node on list
@@ -37,7 +37,7 @@ function LinkedList() {
3737
//check for out-of-bounds values
3838
if (position >= 0 && position <= length){
3939

40-
var node = new Node(element),
40+
let node = new Node(element),
4141
current = head,
4242
previous,
4343
index = 0;
@@ -70,7 +70,7 @@ function LinkedList() {
7070
//check for out-of-bounds values
7171
if (position > -1 && position < length){
7272

73-
var current = head,
73+
let current = head,
7474
previous,
7575
index = 0;
7676

@@ -100,13 +100,13 @@ function LinkedList() {
100100

101101
this.remove = function(element){
102102

103-
var index = this.indexOf(element);
103+
let index = this.indexOf(element);
104104
return this.removeAt(index);
105105
};
106106

107107
this.indexOf = function(element){
108108

109-
var current = head,
109+
let current = head,
110110
index = 0;
111111

112112
while (current) {
@@ -134,7 +134,7 @@ function LinkedList() {
134134

135135
this.toString = function(){
136136

137-
var current = head,
137+
let current = head,
138138
string = '';
139139

140140
while (current) {

chapter05/02-UsingLinkedLists.html

+2-1
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="01-Linked-List.js"></script>
8+
<script type="text/javascript" src="01-LinkedList.js"></script>
9+
<script src="01-LinkedList2.js"></script>
910
<script type="text/javascript" src="02-UsingLinkedLists.js"></script>
1011
</body>
1112
</html>

chapter05/02-UsingLinkedLists.js

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

chapter05/03-Doubly-Linked-List.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
function DoublyLinkedList() {
22

3-
var Node = function(element){
3+
let Node = function(element){
44

55
this.element = element;
66
this.next = null;
77
this.prev = null; //NEW
88
};
99

10-
var length = 0;
11-
var head = null;
12-
var tail = null; //NEW
10+
let length = 0;
11+
let head = null;
12+
let tail = null; //NEW
1313

1414
this.append = function(element){
1515

16-
var node = new Node(element),
16+
let node = new Node(element),
1717
current;
1818

1919
if (head === null){ //first node on list
@@ -35,7 +35,7 @@ function DoublyLinkedList() {
3535
//check for out-of-bounds values
3636
if (position >= 0 && position <= length){
3737

38-
var node = new Node(element),
38+
let node = new Node(element),
3939
current = head,
4040
previous,
4141
index = 0;
@@ -84,7 +84,7 @@ function DoublyLinkedList() {
8484
//check for out-of-bounds values
8585
if (position > -1 && position < length){
8686

87-
var current = head,
87+
let current = head,
8888
previous,
8989
index = 0;
9090

@@ -130,13 +130,13 @@ function DoublyLinkedList() {
130130

131131
this.remove = function(element){
132132

133-
var index = this.indexOf(element);
133+
let index = this.indexOf(element);
134134
return this.removeAt(index);
135135
};
136136

137137
this.indexOf = function(element){
138138

139-
var current = head,
139+
let current = head,
140140
index = -1;
141141

142142
//check first item
@@ -175,7 +175,7 @@ function DoublyLinkedList() {
175175

176176
this.toString = function(){
177177

178-
var current = head,
178+
let current = head,
179179
s = current ? current.element : '';
180180

181181
while(current && current.next){
@@ -188,7 +188,7 @@ function DoublyLinkedList() {
188188

189189
this.inverseToString = function() {
190190

191-
var current = tail,
191+
let current = tail,
192192
s = current ? current.element : '';
193193

194194
while(current && current.prev){

chapter05/04-UsingDoublyLinkedLists.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var list = new DoublyLinkedList();
1+
let list = new DoublyLinkedList();
22

33
list.append(15);
44
list.print();

chapter05/05-CircularLinkedList.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
function CircularLinkedList() {
22

3-
var Node = function(element){
3+
let Node = function(element){
44

55
this.element = element;
66
this.next = null;
77
};
88

9-
var length = 0;
10-
var head = null;
9+
let length = 0;
10+
let head = null;
1111

1212
this.append = function(element){
1313

14-
var node = new Node(element),
14+
let node = new Node(element),
1515
current;
1616

1717
if (head === null){ //first node on list
@@ -40,7 +40,7 @@ function CircularLinkedList() {
4040
//check for out-of-bounds values
4141
if (position >= 0 && position <= length){
4242

43-
var node = new Node(element),
43+
let node = new Node(element),
4444
current = head,
4545
previous,
4646
index = 0;
@@ -84,7 +84,7 @@ function CircularLinkedList() {
8484
//check for out-of-bounds values
8585
if (position > -1 && position < length){
8686

87-
var current = head,
87+
let current = head,
8888
previous,
8989
index = 0;
9090

@@ -121,13 +121,13 @@ function CircularLinkedList() {
121121

122122
this.remove = function(element){
123123

124-
var index = this.indexOf(element);
124+
let index = this.indexOf(element);
125125
return this.removeAt(index);
126126
};
127127

128128
this.indexOf = function(element){
129129

130-
var current = head,
130+
let current = head,
131131
index = -1;
132132

133133
//check first item
@@ -170,7 +170,7 @@ function CircularLinkedList() {
170170

171171
this.toString = function(){
172172

173-
var current = head,
173+
let current = head,
174174
s = current.element;
175175

176176
while(current.next !== head){

chapter05/06-UsingCircularLinkedList.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var circularLinkedList = new CircularLinkedList();
1+
let circularLinkedList = new CircularLinkedList();
22

33
circularLinkedList.append(15);
44
circularLinkedList.print();

chapter07/06-UsingHashCollisionSeparateChaining.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="../chapter05/01-Linked-List.js"></script>
8+
<script type="text/javascript" src="../chapter05/01-LinkedList.js"></script>
99
<script type="text/javascript" src="05-HashCollisionSeparateChaining.js"></script>
1010
<script type="text/javascript" src="06-UsingHashCollisionSeparateChaining.js"></script>
1111
</body>

0 commit comments

Comments
 (0)