-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path14.Queue.js
180 lines (135 loc) · 3.14 KB
/
14.Queue.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
// A FIFO (First In First Out) data structure!
// BIG O of QUEUES
// Insertion - O(1)
// Removal - O(1)
// Searching - O(N)
// Access - O(N)
// LINKED LIST IMPLEMENTATION
class Node {
constructor(value){
this.value = value
this.next = null
}
}
class Queue {
constructor(){
this.first = null; // head
this.last = null; // tail
this.size = 0; // length
}
// add to the end - O(1) time complexity
// push
enqueue(value) {
const newNode = new Node(value)
// if list is empty
if(!this.first) {
this.first = this.last = newNode
this.size++
return this.size
}
// there are some existing nodes in the list
const currentLast = this.last;
currentLast.next = newNode
this.last = newNode
return ++this.size
}
// remove from the beginning - O(1) time complexity
// shift
dequeue() {
// if there is no element in the list
if(!this.first) {
return null
}
// if there is only one element in the list
if(!this.first.next) {
const currentFirst = this.first
this.first = this.last = null
this.size-
return currentFirst.value
}
const currentFirst = this.first;
const newFirst = currentFirst.next
this.first = newFirst
this.size--
return currentFirst.value
}
}
// ARRAY IMPLEMENTATION
class Queue_ {
constructor() {
this.queue = []
}
add(element) {
this.queue.unshift(element)
}
remove(element) {
return this.queue.pop()
}
}
// Queue from Stack
// --- Directions
// Implement a Queue datastructure using two stacks.
// *Do not* create an array inside of the 'Queue' class.
// Queue should implement the methods 'add', 'remove', and 'peek'.
// For a reminder on what each method does, look back
// at the Queue exercise.
// --- Examples
// const q = new Queue();
// q.add(1);
// q.add(2);
// q.peek(); // returns 1
// q.remove(); // returns 1
// q.remove(); // returns 2
class Stack {
constructor() {
this.data = [];
}
push(record) {
this.data.push(record);
}
pop() {
return this.data.pop();
}
peek() {
return this.data[this.data.length - 1];
}
}
// FIFO
class QueueFromStack {
constructor() {
this.firstStack = new Stack();
this.secondStack = new Stack()
}
add(element) {
this.firstStack.push(element)
}
remove() {
while(this.firstStack.peek()) {
this.secondStack.push(this.firstStack.pop())
}
const removedItem = this.secondStack.pop()
while(this.secondStack.peek()) {
this.firstStack.push(this.secondStack.pop())
}
return removedItem
}
peek() {
while(this.firstStack.peek()) {
this.secondStack.push(this.firstStack.pop())
}
const item = this.secondStack.peek()
while(this.secondStack.peek()) {
this.firstStack.push(this.secondStack.pop())
}
return item;
}
}
const q = new QueueFromStack();
q.add(1);
q.add(2);
q.add(3);
q.add(4);
console.log(q.peek()); // returns 1
console.log(q.remove()); // returns 1
console.log(q.remove()); // returns 2
console.log(q.peek()); // returns 3