-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl stack using queue.js
72 lines (61 loc) · 1.16 KB
/
impl stack using 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
class Queue{
constructor(){
this.arr=[];
}
Enqueue(value){
this.arr.push(value);
}
Dequeue(){
if(this.is_empty()){
console.log('under flow');
}
else{
this.arr.shift();
}
}
Front(){
if(this.is_empty()){
console.log('under flow');
}
else return this.arr[0];
}
is_empty(){
let len=this.arr.length-1;
if(len==-1){
return true;
}else return false;
}
display(){
let len=this.arr.length-1;
let ptr=0;
let str='';
while(ptr<=len){
str+=this.arr[ptr]+' ';
ptr++;
}
console.log(str);
}
Push(value){
this.Enqueue(value);
}
Pop(){
this.arr.pop();
}
Top(){
if(!this.is_empty()){
return this.arr[this.arr.length-1];
}else{
console.log('there is no elements in the top');
}
}
}
// driver code
let qu=new Queue();
qu.Push(80);
qu.Push(90);
qu.Push(10);
qu.Push(30);
qu.Push(40);
qu.Pop();
console.log(qu.Top());
qu.display();