-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue-use-linked-list.js
155 lines (133 loc) · 3.14 KB
/
queue-use-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
class ListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
/*
Implementation with one pointer
time complexity:
enqueue - O(n)
dequeue - O(1)
*/
class Queue {
constructor() {
this.head = null;
this.length = 0;
}
isEmpty() {
return this.length === 0;
}
getHead() {
return this.head.val;
}
getLength() {
return this.length;
}
// Insert: add to the end of linked list
// To add an item in the queue, we will check if the list is empty
// then use the new node as the first element else add the new node
// to the end of the existing nodes.
enqueue(item) {
let newNode = new ListNode(item);
if (!this.head) {
this.head = newNode
} else {
let traverseNode = this.head;
while (traverseNode.next) {
traverseNode = traverseNode.next
}
traverseNode.next = newNode;
}
this.length++;
}
// remove from beginning
dequeue() {
if (!this.head) return 'Queue is empty!';
// saves the link to the head which we need to remove
const current = this.head;
// moves the head link to the second Node in the Queue
// this.head is the satisfied customer who has already bought products
// this.head.next is the next customer who becomes the head of the queue after
// satisfied customer leaving
this.head = this.head.next;
this.length--;
// returns the removed Nodes value
return current.val;
}
peek() {
if (!this.head) return 'Queue is empty!';
return this.head.val;
}
// show all values of all nodes in Queue
print() {
let current = this.head;
while (current) {
console.log(current.val);
current = current.next;
}
}
}
/*
Implementation with 2 pointers
time complexity:
enqueue - O(1) because we have tail pointer
dequeue - O(1)
*/
class QueueUse2Pointers {
constructor() {
this.head = null; // link to the first node
this.tail = null; // link to the last node
this.length = 0;
}
isEmpty() {
return this.length === 0;
}
getHead() {
return this.head.val;
}
getLength() {
return this.length;
}
enqueue(val) {
let newNode = new ListNode(val);
if (!this.head) {
this.head = newNode; // the created Node is head
// also the created Node is a tail in Queue because it is single
this.tail = newNode;
} else {
// inserts the created node after the tail
this.tail.next = newNode;
// now created Node is a tail
this.tail = newNode;
}
this.length++;
}
dequeue() {
if (!this.head) return 'Queue is empty!';
const current = this.head;
this.head = this.head.next;
this.length--;
return current.val;
}
peek() {
if (!this.head) return 'Queue is empty!';
return this.head.val;
}
}
// tests
// const persons = new Queue();
// persons.enqueue('first');
// persons.enqueue('second');
// persons.enqueue('third');
// persons.dequeue();
// persons.dequeue();
// persons.dequeue();
// persons.print();
//console.log('Queue', persons)
//const personsJson = JSON.parse(JSON.stringify(persons));
//console.log('personsJson', personsJson);
export {
Queue,
QueueUse2Pointers
}