-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked-List.js
203 lines (140 loc) · 4.7 KB
/
Linked-List.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
// Define a Node class to represent a node in a singly linked list
class Node {
constructor(data) {
this.data = data; // Initialize node data
this.next = null; // Initialize reference to the next node as null
}
}
// Define a SLinkedList class to represent a singly linked list
class SLinkedList {
constructor() {
this.head = null; // Initialize the head of the list as null
this.tail = null; // Initialize the tail of the list as null
}
// Method to add a new node to the end of the list
addNode(data) {
const newNode = new Node(data); // Create a new node with given data
// If the list is empty, set the head as the new node
if (this.head == null) {
this.head = newNode;
} else {
this.tail.next = newNode; // Set the next reference of the tail to the new node
}
this.tail = newNode; // Set the new node as the tail of the list
}
// Method to display the list
display() {
// If the list is empty, print a message and return
if (this.head == null) {
const result = "List is Empty.";
console.log(result);
return result;
}
let temp = this.head; // Start from the head node
while (temp != null) { // Iterate through the list
console.log(temp.data + "=>"); // Print the data of each node
temp = temp.next; // Move to the next node
}
if (temp == null) { // If we have reached the end of the list, print a message
const result = "Completed printing the list.";
console.log(result);
return result;
}
}
// Method to delete a node with given data from the list
deleteNode(data) {
// If the list is empty
if (this.head == null) {
this.tail = null; // Update the tail as there are no nodes left
const result = "List is Empty.";
console.log(result);
return result;
}
let temp = this.head; // Start from the head node
let prev = null; // Initialize a variable to keep track of the previous node
// If the head node itself contains the data to be deleted, update the head and return
if (temp != null && temp.data == data) {
this.head = temp.next;
const result = "Deleted head of the list.";
console.log(result);
return result;
}
// Iterate through the list to find the node with given data
while (temp != null && temp.data != data) {
prev = temp;
temp = temp.next;
}
if (temp == null) { // If the data was not found, return
const result = "Target Data not found in the list."
console.log(result);
return result;
}
// If the tail node contains the data to be deleted, update the tail and return
if (temp == this.tail) {
this.tail = prev;
this.tail.next = null;
return;
}
/*
If a Node in the list which is not head or not tail and has the target data to delete.
Remove the node with target data by updating the next reference of the previous node.
*/
prev.next = temp.next; // This will delete the node after prev (ie, the node before temp) that has the target data to delete from the list.
}
// Method to reverse a linked list
reverseLinkedList() {
let currentNode = this.head;
this.head = this.tail;
this.tail = currentNode;
let nextNode = null;
let prevNode = null;
while (currentNode !== null){
nextNode = currentNode.next;
currentNode.next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
}
countNodes(){
let count = 0;
if(this.head == null){
const result = "Empty List";
console.log(result);
return result;
}
let currentNode = this.head;
while(currentNode !== null){
currentNode = currentNode.next;
count++;
}
const result = `The total number of nodes in the given list is: ${count}`;
console.log(result);
return result;
}
}
// =============Creating LINKED LISTS and processing various operations in a LINKED LIST=============
// Creating two linked lists
const list1 = new SLinkedList();
const list2 = new SLinkedList();
// Add nodes to the first linked list
list1.addNode(10);
list1.addNode(20);
list1.addNode(30);
list1.addNode(40);
// Display the first linked list
// list1.display();
// Add nodes to the second linked list
list2.addNode(100);
list2.addNode(200);
list2.addNode(300);
list2.addNode(400);
// Display the Second list
// list2.display();
// Delete a node from the first linked list
// list2.deleteNode(300); // Deleting the node with the value 300
// Display the Second linked list
list2.display();
// Reversing a linked list
list2.reverseLinkedList();
// Displaying the reversed linked list
list2.display();