class Stack<T> {
private items: Map<number, T>;
constructor() {
this.items = new Map();
}
public push(element: T) {
this.items.set(this.items.size, element);
}
public pop(): T {
if (this.isEmpty()) {
return undefined;
}
const result = this.items.get(this.items.size - 1);
this.items.delete(this.items.size - 1);
return result;
}
public peek(): T {
if (this.isEmpty()) {
return undefined;
}
return this.items.get(this.items.size - 1);
}
public isEmpty(): boolean {
return this.items.size === 0;
}
public size(): number {
return this.items.size;
}
public clear() {
this.items.clear();
}
public toString(): string {
if (this.isEmpty()) {
return "";
}
let result: string = "";
this.items.forEach((value, key) => {
result = `${result}${key === 0 ? "" : ","}${value}`;
});
return result;
}
}