-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue_js.txt
39 lines (38 loc) · 895 Bytes
/
Queue_js.txt
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
/*implementing queue*/
class Queue {
constructor() {
this.elements = [];
this.elem_count = 0;
this.count_after_pop=-1;
}
push(item) {
this.elements[this.elem_count] = item;
++this.elem_count;
}
pop() {
if (this.elem_count-this.count_after_pop === 0)
return "underflow";
else {
let poped_elem = this.elements[this.count_after_pop];
++this.count_after_pop;
delete this.elements[this.count_after_pop];
return poped_elem;
}
}
peek() {
if (this.elem_count === 0)
return "underflow";
else
return this.elements[this.elem_count - 1];
}
reverse() {
let reverced_arr = [];
for (let i = this.count_after_pop; i < this.elem_count; ++i) {
reverced_arr[i] = this.elements[this.elem_count - i - 1];
}
this.elements = reverced_arr;
}
empty() {
return this.elem_count === 0;
}
}