-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack2.js
47 lines (44 loc) · 880 Bytes
/
stack2.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
class Stack{
constructor(cap){
this.cap=cap;
this.arr=[];
this.top=-1;
}
push(i){
if(this.top==this.cap-1){
console.log("Overflow");
}
this.top++;
this.arr[this.top]=i;
console.log(i,"pushed");
this.print();
}
pop(){
if(this.top==-1){
console.log("underflow");
}
let res=this.arr[this.top];
this.top--;
console.log(res, "popped");
this.arr.pop()
this.print()
}
peek(){
if(this.top==-1){
console.log('Enmpty')
}
console.log("Top of the stack is :",this.arr[this.top]);
this.print();
}
print(){
console.log("Stack is",this.arr);
}
}
let s1=new Stack(4);
s1.push(4);
s1.peek();
s1.push(5);
s1.push(8);
s1.push(1);
s1.push(9);
s1.pop();