-
Notifications
You must be signed in to change notification settings - Fork 1k
/
QueueLinkedList.js
180 lines (145 loc) · 4.24 KB
/
QueueLinkedList.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
/*
A queue is a linear data structure that follows the First in, First out principle (i.e. the first added elements are removed first).
Generally, a queue is implemented using Linked List but other approach can be used i.e using array ( which has high time complexity ) or using stack.
Below is the implementation of a queue using a linked list ( Singly Linked List ).
In this program, the user can provide a choice to perform operations on a Queue. It is created using an ES6 class syntax.
prompt & colors are node packages for taking user input and displaying text in different colors respectively.
*/
const prompt = require("prompt-sync")({ sigint: true });
const colors = require("colors");
// Setting the Color schema
colors.setTheme({
display: "green",
insert: "white",
delete: "red",
leave: "yellow",
end: "rainbow",
wrong: "america",
});
// Creating the node class to create nodes
class Node {
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}
// Creating a Queue class to perform operations
class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
// Get the first element of Queue
peek() {
if (this.length === 0) return console.log("Empty Queue");
return console.log(this.first.value);
}
// Insert an element in the Queue
enqueue(value) {
const newNode = new Node(value);
if (this.length === 0) {
this.first = newNode;
this.last = newNode;
} else {
this.last.next = newNode;
this.last = newNode;
}
this.length++;
return this;
}
// Delete an element from the Queue
dequeue() {
if (!this.first) return console.log("Queue is already empty");
if (this.first === this.last) {
this.first = null;
this.last = null;
} else {
this.first = this.first.next;
}
this.length--;
return this;
}
// Check whether the Queue is empty or not
isEmpty() {
if (this.length === 0) return console.log("Into the Void here");
return console.log("There is something deep inside it");
}
// Print all the elements of Queue
printList() {
let currentNode = this.first;
let queueItems = [];
while (currentNode) {
queueItems.push(currentNode.value);
currentNode = currentNode.next;
}
return console.log("Queue", queueItems);
}
}
// Instantiating the Queue class
const myQueue = new Queue();
let choice = 0;
let value;
// Asking user input for performing operations on Queue
do {
console.log("\nWelcome to the Queue Linked List Show");
console.log("Follow the instructions to get in the show");
console.log("0. To exit without doing anything".yellow);
console.log("1. Display the queue".display);
console.log("2. Insert element in queue".insert);
console.log("3. Get first element of queue".display);
console.log("4. Check whether queue in empty or not".insert);
console.log("5. Delete element from queue".delete);
choice = +prompt("Enter your choice - ");
switch (choice) {
case 0:
console.log("Hmm, matter of choice!\n".rainbow);
return;
case 1:
myQueue.printList();
break;
case 2:
value = prompt("Enter element to insert into the queue - ");
myQueue.enqueue(value);
myQueue.printList();
break;
case 3:
myQueue.peek();
break;
case 4:
myQueue.isEmpty();
break;
case 5:
myQueue.dequeue();
myQueue.printList();
break;
default:
console.log("You know this is wrong, Bbye!\n".wrong);
return;
}
} while (choice !== 0);
// Sample I/O of the above program -
/*
> node QueueLinkedList
Welcome to the Queue Linked List Show
Follow the instructions to get in the show
0. To exit without doing anything
1. Display the queue
2. Insert element in queue
3. Get first element of queue
4. Check whether queue in empty or not
5. Delete element from queue
Enter your choice - 1
Queue []
Welcome to the Queue Linked List Show
Follow the instructions to get in the show
0. To exit without doing anything
1. Display the queue
2. Insert element in queue
3. Get first element of queue
4. Check whether queue in empty or not
5. Delete element from queue
Enter your choice - 2
Enter element to insert into the queue - 4
Queue [ '4' ]
*/