forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSinglyLinkList.js
209 lines (178 loc) · 4.9 KB
/
SinglyLinkList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* SinglyLinkedList!!
* A linked list is implar to an array, it hold values.
* However, links in a linked list do not have indexes. With
* a linked list you do not need to predetermine it's size as
* it grows and shrinks as it is edited. This is an example of
* a singly linked list.
*/
// Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
// class LinkedList and constructor
// Creates a LinkedList
var LinkedList = (function () {
function LinkedList () {
// Length of linklist and head is null at start
this.length = 0
this.head = null
}
// class node (constructor)
// Creating Node with element's value
var Node = (function () {
function Node (element) {
this.element = element
this.next = null
}
return Node
}())
// Returns length
LinkedList.prototype.size = function () {
return this.length
}
// Returns the head
LinkedList.prototype.head = function () {
return this.head
}
// Creates a node and adds it to linklist
LinkedList.prototype.add = function (element) {
var node = new Node(element)
// Check if its the first element
if (this.head === null) {
this.head = node
} else {
var currentNode = this.head
// Loop till there is node present in the list
while (currentNode.next) {
currentNode = currentNode.next
}
// Adding node to the end of the list
currentNode.next = node
}
// Increment the length
this.length++
}
// Removes the node with the value as param
LinkedList.prototype.remove = function (element) {
var currentNode = this.head
var previousNode
// Check if the head node is the element to remove
if (currentNode.element === element) {
this.head = currentNode.next
} else {
// Check which node is the node to remove
while (currentNode.element !== element) {
previousNode = currentNode
currentNode = currentNode.next
}
// Removing the currentNode
previousNode.next = currentNode.next
}
// Decrementing the length
this.length--
}
// Return if the list is empty
LinkedList.prototype.isEmpty = function () {
return this.length === 0
}
// Returns the index of the element passed as param otherwise -1
LinkedList.prototype.indexOf = function (element) {
var currentNode = this.head
var index = -1
while (currentNode) {
index++
// Checking if the node is the element we are searching for
if (currentNode.element === element) {
return index + 1
}
currentNode = currentNode.next
}
return -1
}
// Returns the element at an index
LinkedList.prototype.elementAt = function (index) {
var currentNode = this.head
var count = 0
while (count < index) {
count++
currentNode = currentNode.next
}
return currentNode.element
}
// Adds the element at specified index
LinkedList.prototype.addAt = function (index, element) {
index--
var node = new Node(element)
var currentNode = this.head
var previousNode
var currentIndex = 0
// Check if index is out of bounds of list
if (index > this.length) {
return false
}
// Check if index is the start of list
if (index === 0) {
node.next = currentNode
this.head = node
} else {
while (currentIndex < index) {
currentIndex++
previousNode = currentNode
currentNode = currentNode.next
}
// Adding the node at specified index
node.next = currentNode
previousNode.next = node
}
// Incrementing the length
this.length++
return true
}
// Removes the node at specified index
LinkedList.prototype.removeAt = function (index) {
index--
var currentNode = this.head
var previousNode
var currentIndex = 0
// Check if index is present in list
if (index < 0 || index >= this.length) {
return null
}
// Check if element is the first element
if (index === 0) {
this.head = currentNode.next
} else {
while (currentIndex < index) {
currentIndex++
previousNode = currentNode
currentNode = currentNode.next
}
previousNode.next = currentNode.next
}
// Decrementing the length
this.length--
return currentNode.element
}
// Function to view the LinkedList
LinkedList.prototype.view = function () {
var currentNode = this.head
var count = 0
while (count < this.length) {
count++
console.log(currentNode.element)
currentNode = currentNode.next
}
}
// returns the constructor
return LinkedList
}())
// Implementation of LinkedList
var linklist = new LinkedList()
linklist.add(2)
linklist.add(5)
linklist.add(8)
linklist.add(12)
linklist.add(17)
console.log(linklist.size())
console.log(linklist.removeAt(4))
linklist.addAt(4, 15)
console.log(linklist.indexOf(8))
console.log(linklist.size())
linklist.view()