-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.ts
59 lines (47 loc) · 1.02 KB
/
index.ts
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
// https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
export interface Stack<T> {
items: T[];
maximum: number;
top: number;
peek(): T;
push(item: T): void;
pop(): T;
}
export class Stack<T> implements Stack<T> {
items: T[];
maximum: number;
constructor(maximum: number = Math.pow(2, 32) - 1) {
this.items = new Array(maximum);
this.maximum = maximum;
this.top = 0;
}
peek() {
return this.items[this.top - 1];
}
push(item: T) {
if (this.top === this.maximum) {
throw Error("Overflow error");
}
this.items[this.top++] = item;
}
pop() {
if (this.top === 0) {
throw Error("Underflow error");
}
this.top--;
const removed = this.items[this.top];
delete this.items[this.top];
return removed;
}
}
// const stack = new Stack<number>(64);
// for (let i = 0; i < stack.maximum; i++) {
// stack.push(i);
// }
// // 63
// console.log(stack.peek());
// // 64
// console.log(stack.top);
// // 63
// stack.pop();
// console.log(stack.top);