-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathlinkedList_Implementation.js
More file actions
161 lines (146 loc) · 3.86 KB
/
linkedList_Implementation.js
File metadata and controls
161 lines (146 loc) · 3.86 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
// Code to implement a linked list in JavaScript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Linked List Implementation
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
// Adds node to the end of the linked list
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
this.size++;
}
// Adds node to the beginning of the linked list
prepend(data) {
const newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
this.size++;
}
// Removes node from the linked list
remove(data) {
if (!this.head) return;
if (this.head.data === data) {
this.head = this.head.next;
this.size--;
return;
}
let current = this.head;
let prev = null;
while (current && current.data !== data) {
prev = current;
current = current.next;
}
if (current) {
prev.next = current.next;
this.size--;
}
}
// Checks if the linked list contains a node with the given data
contains(data) {
let current = this.head;
while (current) {
if (current.data === data) {
return true;
}
current = current.next;
}
return false;
}
// Returns the size of the linked list
getSize() {
return this.size;
}
// Returns the linked list as an array
toArray() {
const result = [];
let current = this.head;
while (current) {
result.push(current.data);
current = current.next;
}
return result;
}
}
// Function to read user input
function getUserInput() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const linkedList = new LinkedList();
function promptAction() {
console.log('Linked List Operations:');
console.log('1. Append');
console.log('2. Prepend');
console.log('3. Remove');
console.log('4. Check for Containment');
console.log('5. Get Size');
console.log('6. Display Linked List');
console.log('7. Exit');
readline.question('Select an operation (1-7): ', (choice) => {
switch (choice) {
case '1':
readline.question('Enter data to append: ', (data) => {
linkedList.append(data);
promptAction();
});
break;
case '2':
readline.question('Enter data to prepend: ', (data) => {
linkedList.prepend(data);
promptAction();
});
break;
case '3':
readline.question('Enter data to remove: ', (data) => {
linkedList.remove(data);
promptAction();
});
break;
case '4':
readline.question('Enter data to check for containment: ', (data) => {
const contains = linkedList.contains(data);
console.log(`Contains ${data}: ${contains}`);
promptAction();
});
break;
case '5':
const size = linkedList.getSize();
console.log(`Size of the Linked List: ${size}`);
promptAction();
break;
case '6':
const array = linkedList.toArray();
console.log(`Linked List: ${array.join(' -> ')}`);
promptAction();
break;
case '7':
readline.close();
break;
default:
console.log('Invalid choice. Please select an operation (1-7).');
promptAction();
break;
}
});
}
promptAction();
}
// Call the function to read user input
getUserInput();