Stack
O(1)O(1)
3
1
2
Code:
class Stack<T> {
items: T[];
constructor() {
this.items = [];
}
push(element: T) {
this.items.push(element);
}
pop() {
if (this.isEmpty()) {
return null;
}
return this.items.pop();
}
peek() {
if (this.isEmpty()) {
return null;
}
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
};