-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.js
More file actions
59 lines (51 loc) · 1014 Bytes
/
StackArray.js
File metadata and controls
59 lines (51 loc) · 1014 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
53
54
55
56
57
58
59
// push/pop/isEmpty/isFull/top
class Stack {
constructor(capacity) {
this.list = [];
this.capacity = capacity;
this.length = 0;
}
push(item) {
if (this.length < this.capacity) {
this.list.push(item);
this.length++;
}
}
pop() {
if (this.length != 0) {
this.list.pop();
this.length--;
}
}
isEmpty() {
return this.length == 0 ? true : false;
}
isFull() {
return this.length < this.capacity ? false : true;
}
top() {
return this.length != 0 ? this.list[this.length - 1] : null;
}
print() {
// this.list.forEach((item)=>{
// console.log(item)
// })
console.log(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