-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackObject.js
More file actions
52 lines (48 loc) · 924 Bytes
/
StackObject.js
File metadata and controls
52 lines (48 loc) · 924 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
50
51
52
class Stack {
constructor(capacity) {
this.list = {};
this.capacity = capacity;
this.length = 0;
}
push(item) {
if (this.length < this.capacity) {
this.list[this.length + 1] = item;
this.length++;
}
}
pop() {
if (this.length != 0) {
delete this.list[this.length];
this.length--;
}
}
isEmpty() {
return this.length == 0 ? true : false;
}
isFull() {
return this.length == this.capacity;
}
top(){
if(this.length!=0)
return this.list[this.length]
}
print() {
console.log(Object.values(this.list));
}
}
const stack = new Stack(5);
stack.print();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.print();
stack.push(60);
stack.print();
stack.pop();
stack.pop();
stack.print();
console.log(stack.isEmpty()); //false
console.log(stack.isFull()); //true
console.log(stack.top()); //undefined