class Stack {
constructor() {
this.items = [];
}
add(element) {
return this.items.push(element);
}
size() {
return this.items.length;
}
pop() {
if (this.items.length > 0) {
return this.items.pop();
}
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length == 0;
}
print() {
for (let i = 0; i < this.items.length; i++) {
console.log(this.items[i]);
}
}
delete() {
this.items = [];
}
}
let stack = new Stack();
stack.add(5);
stack.add(4);
stack.add(8);
stack.add(7);
stack.add(9);
stack.add(0);
console.log(stack.items);
stack.pop();
console.log(stack.items);
console.log(stack.peek());
console.log("size: " + stack.size());
console.log(stack.isEmpty());
stack.pop();
stack.print();
stack.delete();
console.log(stack.items);
/*
run:
[ 5, 4, 8, 7, 9, 0 ]
[ 5, 4, 8, 7, 9 ]
9
size: 5
false
5
4
8
7
[]
*/