-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjStack.js
More file actions
49 lines (39 loc) · 672 Bytes
/
objStack.js
File metadata and controls
49 lines (39 loc) · 672 Bytes
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
class Stack {
constructor() {
this.items={}
this.size =0
}
push(element){
this.items[this.size]=element
this.size++;
}
pop(){
if(this.isEmpty()){
return "Undeflow"
}
this.size--
const poppedElement = this.items[this.size];
delete this.items[this.size]
return poppedElement;
}
isEmpty(){
return this.size ===0
}
size(){
return this.size;
}
peek(){
return this.isEmpty()?'Underflow': this.items[this.size-1]
}
print(){
console.log(this.items);
}
}
const stack = new Stack
stack.push(10)
stack.push(20)
stack.push(30)
stack.push(40)
stack.print()
stack.pop()
stack.print( )