-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglyLinkedList.js
More file actions
226 lines (220 loc) · 4.94 KB
/
Copy pathsinglyLinkedList.js
File metadata and controls
226 lines (220 loc) · 4.94 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* The above class defines a singly linked list data structure in JavaScript with methods for basic
operations like prepend, append, insert, remove, search, reverse, clear, and print. */
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
export class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
// isEmpty
isEmpty() {
return this.size === 0;
}
// get size
getSize() {
return this.size;
}
// prepend
prepend(data) {
const node = new Node(data);
if (this.isEmpty()) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head = node;
}
this.size++;
}
// append
append(data) {
if (this.isEmpty()) {
return this.prepend(data);
}
const node = new Node(data);
this.tail.next = node;
this.tail = node;
this.size++;
}
// remove from head
removeHead() {
if (this.isEmpty()) {
return null;
} else {
const removedNode = this.head;
this.head = this.head.next;
this.size--;
return removedNode.data;
}
}
// remove from tail
removeTail() {
if (this.isEmpty()) {
return null;
}
const removedNode = this.tail;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = this.tail.next;
this.size--;
return removedNode.data;
}
}
// insert
insert(data, index) {
if (index < 0 || index >= this.size) {
return console.log("Index out of bounds");
}
if (index === 0) {
return this.prepend(data);
}
const node = new Node(data);
let current = this.head;
let previous;
let count = 0;
while (count < index) {
previous = current;
current = current.next;
count++;
}
node.next = current;
previous.next = node;
this.size++;
}
// get index
getIndex(index) {
if (index < 0 || index >= this.size) {
return console.log("Index out of bounds");
}
let current = this.head;
let count = 0;
while (count < index) {
current = current.next;
count++;
}
return console.log("data at index " + index + ": ", current.data);
}
// search
search(data) {
if (this.isEmpty()) {
return console.log("List is empty");
}
let current = this.head;
let index = 0;
while (current) {
if (current.data === data) {
return console.log("data found at index: ", index);
}
current = current.next;
index++;
}
return console.log("data not found in the list");
}
// remove by index
removeIndex(index) {
if (index < 0 || index >= this.size) {
return console.log("Index out of bounds");
}
if (index === 0) {
const removedNode = this.head;
this.head = this.head.next;
this.size--;
return console.log("Removed data: ", removedNode.data);
}
let current = this.head;
let previous;
let count = 0;
while (count < index) {
previous = current;
current = current.next;
count++;
}
previous.next = current.next;
this.size--;
return console.log("Removed data: ", current.data);
}
// remove by data
removeData(data) {
if (this.isEmpty()) {
return console.log("List is empty");
}
let current = this.head;
let previous;
while (current) {
if (current.data === data) {
if (current === this.head) {
this.head = this.head.next;
} else {
previous.next = current.next;
}
this.size--;
return console.log("Removed data: ", current.data);
}
previous = current;
current = current.next;
}
}
// reverse
reverse() {
if (this.isEmpty()) {
return console.log("List is empty");
}
let current = this.head;
let previous = null;
let next;
while (current) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
this.head = previous;
}
// clear
clear() {
this.head = null;
this.tail = null;
this.size = 0;
}
// print
print() {
if (this.isEmpty()) {
console.log("List is empty");
}
let current = this.head;
while (current) {
console.log(current.data);
current = current.next;
}
}
}
// const list = new SinglyLinkedList();
// console.log("List is empty: ", list.isEmpty());
// console.log("List size: ", list.getSize());
// list.prepend(10);
// list.prepend(20);
// list.append(5);
// list.append(15);
// list.print();
// console.log("------------------------------");
// list.insert(22, 2);
// list.getIndex(3);
// list.search(5);
// console.log("------------------------------");
// list.print();
// list.removeHead(2);
// list.removeTail(15);
// list.print();
// list.reverse();
// list.print();
// list.clear();
// console.log("List is empty: ", list.isEmpty());
// console.log("List size: ", list.getSize());