Skip to content

Commit 961f21f

Browse files
merge: Add test case and enacements (TheAlgorithms#858)
1 parent c3b2bac commit 961f21f

File tree

2 files changed

+270
-85
lines changed

2 files changed

+270
-85
lines changed

Data-Structures/Linked-List/SinglyLinkList.js

Lines changed: 104 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,40 @@
66
* a singly linked list.
77
*/
88

9-
// Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
9+
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get
1010

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
1815
}
16+
}
1917

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+
}
2923

3024
// Returns length
31-
LinkedList.prototype.size = function () {
25+
size () {
3226
return this.length
3327
}
3428

3529
// Returns the head
36-
LinkedList.prototype.head = function () {
37-
return this.head
30+
head () {
31+
return this.headNode ? this.headNode.data : null
3832
}
3933

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) {
4236
const node = new Node(element)
37+
4338
// Check if its the first element
44-
if (this.head === null) {
45-
this.head = node
39+
if (this.headNode === null) {
40+
this.headNode = node
4641
} else {
47-
let currentNode = this.head
42+
let currentNode = this.headNode
4843

4944
// Loop till there is a node present in the list
5045
while (currentNode.next) {
@@ -54,144 +49,168 @@ const LinkedList = (function () {
5449
// Adding node at the end of the list
5550
currentNode.next = node
5651
}
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+
}
5988
}
6089

6190
// 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
6593

6694
// 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
6997
} else {
7098
// 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+
}
73104
currentNode = currentNode.next
74105
}
75-
76-
// Removing the currentNode
77-
previousNode.next = currentNode.next
78106
}
79107

80-
// Decrementing the length
81-
this.length--
108+
this.length-- // Decrementing the length
82109
}
83110

84111
// Return if the list is empty
85-
LinkedList.prototype.isEmpty = function () {
112+
isEmpty () {
86113
return this.length === 0
87114
}
88115

89116
// 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
93120

94121
while (currentNode) {
95-
index++
96-
97122
// 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
100125
}
101126
currentNode = currentNode.next
127+
index++
102128
}
103129

104130
return -1
105131
}
106132

107133
// 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
110139
let count = 0
111140
while (count < index) {
112141
count++
113142
currentNode = currentNode.next
114143
}
115-
return currentNode.element
144+
return currentNode.data
116145
}
117146

118147
// Adds the element at specified index
119-
LinkedList.prototype.addAt = function (index, element) {
120-
index--
148+
addAt (index, element) {
121149
const node = new Node(element)
122150

123-
let currentNode = this.head
124-
let previousNode
125-
let currentIndex = 0
126-
127151
// 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')
130154
}
131155

156+
let currentNode = this.headNode
157+
let currentIndex = 0
132158
// Check if index is the start of list
133159
if (index === 0) {
134160
node.next = currentNode
135-
this.head = node
161+
this.headNode = node
136162
} else {
137-
while (currentIndex < index) {
163+
while (currentIndex !== index - 1) {
138164
currentIndex++
139-
previousNode = currentNode
140165
currentNode = currentNode.next
141166
}
142167

143168
// 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
146172
}
147173

148174
// Incrementing the length
149175
this.length++
150-
return true
151176
}
152177

153178
// 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
158181
let currentIndex = 0
159182

160183
// Check if index is present in list
161184
if (index < 0 || index >= this.length) {
162-
return null
185+
throw new RangeError('Out of Range index')
163186
}
164187

165188
// Check if element is the first element
166189
if (index === 0) {
167-
this.head = currentNode.next
190+
this.headNode = currentNode.next
168191
} else {
169-
while (currentIndex < index) {
192+
while (currentIndex !== index - 1) {
170193
currentIndex++
171-
previousNode = currentNode
172194
currentNode = currentNode.next
173195
}
174-
previousNode.next = currentNode.next
196+
currentNode.next = currentNode.next.next
175197
}
176198

177199
// Decrementing the length
178200
this.length--
179-
return currentNode.element
180201
}
181202

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)
189209
currentNode = currentNode.next
190210
}
191-
}
192211

193-
// returns the constructor
194-
return LinkedList
195-
}())
212+
return list
213+
}
214+
}
196215

197216
export { LinkedList }

0 commit comments

Comments
 (0)