6
6
* a singly linked list.
7
7
*/
8
8
9
- // Functions - add, remove, indexOf, elementAt , addAt, removeAt, view
9
+ // Methods - size, head, addLast, addFirst , addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get
10
10
11
- // class LinkedList and constructor
12
- // Creates a LinkedList
13
- const LinkedList = ( function ( ) {
14
- function LinkedList ( ) {
15
- // Length of linklist and head is null at start
16
- this . length = 0
17
- this . head = null
11
+ class Node {
12
+ constructor ( data ) {
13
+ this . data = data
14
+ this . next = null
18
15
}
16
+ }
19
17
20
- // Class node (constructor)
21
- // Creating Node with element's value
22
- const Node = ( function ( ) {
23
- function Node ( element ) {
24
- this . element = element
25
- this . next = null
26
- }
27
- return Node
28
- } ( ) )
18
+ class LinkedList {
19
+ constructor ( ) {
20
+ this . headNode = null
21
+ this . length = 0
22
+ }
29
23
30
24
// Returns length
31
- LinkedList . prototype . size = function ( ) {
25
+ size ( ) {
32
26
return this . length
33
27
}
34
28
35
29
// Returns the head
36
- LinkedList . prototype . head = function ( ) {
37
- return this . head
30
+ head ( ) {
31
+ return this . headNode ? this . headNode . data : null
38
32
}
39
33
40
- // Creates a node and adds it to linklist
41
- LinkedList . prototype . add = function ( element ) {
34
+ // add a node at last it to linklist
35
+ addLast ( element ) {
42
36
const node = new Node ( element )
37
+
43
38
// Check if its the first element
44
- if ( this . head === null ) {
45
- this . head = node
39
+ if ( this . headNode === null ) {
40
+ this . headNode = node
46
41
} else {
47
- let currentNode = this . head
42
+ let currentNode = this . headNode
48
43
49
44
// Loop till there is a node present in the list
50
45
while ( currentNode . next ) {
@@ -54,144 +49,168 @@ const LinkedList = (function () {
54
49
// Adding node at the end of the list
55
50
currentNode . next = node
56
51
}
57
- // Increment the length
58
- this . length ++
52
+
53
+ this . length ++ // Increment the length
54
+ }
55
+
56
+ // add a node at first it to linklist
57
+ addFirst ( element ) {
58
+ const node = new Node ( element )
59
+ node . next = this . headNode
60
+ this . headNode = node
61
+ this . length ++ // Increment the length
62
+ }
63
+
64
+ // remove the first from the linklist
65
+ removeFirst ( ) {
66
+ if ( this . length > 0 ) {
67
+ this . headNode = this . headNode . next
68
+ this . length --
69
+ }
70
+ }
71
+
72
+ // remove the last from the linklist
73
+ removeLast ( ) {
74
+ if ( this . length === 1 ) {
75
+ this . headNode = null
76
+ this . length --
77
+ } else if ( this . length > 1 ) {
78
+ let index = 0
79
+ let currentNode = this . headNode
80
+ while ( index !== this . length - 2 ) {
81
+ index ++
82
+ currentNode = currentNode . next
83
+ }
84
+
85
+ currentNode . next = null
86
+ this . length --
87
+ }
59
88
}
60
89
61
90
// Removes the node with the value as param
62
- LinkedList . prototype . remove = function ( element ) {
63
- let currentNode = this . head
64
- let previousNode
91
+ remove ( element ) {
92
+ let currentNode = this . headNode
65
93
66
94
// Check if the head node is the element to remove
67
- if ( currentNode . element === element ) {
68
- this . head = currentNode . next
95
+ if ( currentNode . data === element ) {
96
+ this . headNode = currentNode . next
69
97
} else {
70
98
// Check which node is the node to remove
71
- while ( currentNode . element !== element ) {
72
- previousNode = currentNode
99
+ while ( currentNode && currentNode . next ) {
100
+ if ( currentNode . next . data === element ) {
101
+ currentNode . next = currentNode . next . next
102
+ break
103
+ }
73
104
currentNode = currentNode . next
74
105
}
75
-
76
- // Removing the currentNode
77
- previousNode . next = currentNode . next
78
106
}
79
107
80
- // Decrementing the length
81
- this . length --
108
+ this . length -- // Decrementing the length
82
109
}
83
110
84
111
// Return if the list is empty
85
- LinkedList . prototype . isEmpty = function ( ) {
112
+ isEmpty ( ) {
86
113
return this . length === 0
87
114
}
88
115
89
116
// Returns the index of the element passed as param otherwise -1
90
- LinkedList . prototype . indexOf = function ( element ) {
91
- let currentNode = this . head
92
- let index = - 1
117
+ indexOf ( element ) {
118
+ let currentNode = this . headNode
119
+ let index = 0
93
120
94
121
while ( currentNode ) {
95
- index ++
96
-
97
122
// Checking if the node is the element we are searching for
98
- if ( currentNode . element === element ) {
99
- return index + 1
123
+ if ( currentNode . data === element ) {
124
+ return index
100
125
}
101
126
currentNode = currentNode . next
127
+ index ++
102
128
}
103
129
104
130
return - 1
105
131
}
106
132
107
133
// Returns the element at an index
108
- LinkedList . prototype . elementAt = function ( index ) {
109
- let currentNode = this . head
134
+ elementAt ( index ) {
135
+ if ( index >= this . length || index < 0 ) {
136
+ throw new RangeError ( 'Out of Range index' )
137
+ }
138
+ let currentNode = this . headNode
110
139
let count = 0
111
140
while ( count < index ) {
112
141
count ++
113
142
currentNode = currentNode . next
114
143
}
115
- return currentNode . element
144
+ return currentNode . data
116
145
}
117
146
118
147
// Adds the element at specified index
119
- LinkedList . prototype . addAt = function ( index , element ) {
120
- index --
148
+ addAt ( index , element ) {
121
149
const node = new Node ( element )
122
150
123
- let currentNode = this . head
124
- let previousNode
125
- let currentIndex = 0
126
-
127
151
// Check if index is out of bounds of list
128
- if ( index > this . length ) {
129
- return false
152
+ if ( index > this . length || index < 0 ) {
153
+ throw new RangeError ( 'Out of Range index' )
130
154
}
131
155
156
+ let currentNode = this . headNode
157
+ let currentIndex = 0
132
158
// Check if index is the start of list
133
159
if ( index === 0 ) {
134
160
node . next = currentNode
135
- this . head = node
161
+ this . headNode = node
136
162
} else {
137
- while ( currentIndex < index ) {
163
+ while ( currentIndex !== index - 1 ) {
138
164
currentIndex ++
139
- previousNode = currentNode
140
165
currentNode = currentNode . next
141
166
}
142
167
143
168
// Adding the node at specified index
144
- node . next = currentNode
145
- previousNode . next = node
169
+ const temp = currentNode . next
170
+ currentNode . next = node
171
+ node . next = temp
146
172
}
147
173
148
174
// Incrementing the length
149
175
this . length ++
150
- return true
151
176
}
152
177
153
178
// Removes the node at specified index
154
- LinkedList . prototype . removeAt = function ( index ) {
155
- index --
156
- let currentNode = this . head
157
- let previousNode
179
+ removeAt ( index ) {
180
+ let currentNode = this . headNode
158
181
let currentIndex = 0
159
182
160
183
// Check if index is present in list
161
184
if ( index < 0 || index >= this . length ) {
162
- return null
185
+ throw new RangeError ( 'Out of Range index' )
163
186
}
164
187
165
188
// Check if element is the first element
166
189
if ( index === 0 ) {
167
- this . head = currentNode . next
190
+ this . headNode = currentNode . next
168
191
} else {
169
- while ( currentIndex < index ) {
192
+ while ( currentIndex !== index - 1 ) {
170
193
currentIndex ++
171
- previousNode = currentNode
172
194
currentNode = currentNode . next
173
195
}
174
- previousNode . next = currentNode . next
196
+ currentNode . next = currentNode . next . next
175
197
}
176
198
177
199
// Decrementing the length
178
200
this . length --
179
- return currentNode . element
180
201
}
181
202
182
- // Function to view the LinkedList
183
- LinkedList . prototype . view = function ( output = value => console . log ( value ) ) {
184
- let currentNode = this . head
185
- let count = 0
186
- while ( count < this . length ) {
187
- count ++
188
- output ( currentNode . element )
203
+ // Method to get the LinkedList
204
+ get ( ) {
205
+ const list = [ ]
206
+ let currentNode = this . headNode
207
+ while ( currentNode ) {
208
+ list . push ( currentNode . data )
189
209
currentNode = currentNode . next
190
210
}
191
- }
192
211
193
- // returns the constructor
194
- return LinkedList
195
- } ( ) )
212
+ return list
213
+ }
214
+ }
196
215
197
216
export { LinkedList }
0 commit comments