-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.ts
More file actions
36 lines (29 loc) · 696 Bytes
/
Copy pathstack.ts
File metadata and controls
36 lines (29 loc) · 696 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
interface StackInterface<T> {
data: T[];
push(item: T): void;
pop(): T | null | undefined;
}
class Stack<T> implements StackInterface<T> {
data: T[];
constructor(initialData: T[]) {
this.data = initialData;
}
push(item: T) {
this.data.push(item);
}
pop() {
if (this.data.length) {
return this.data.pop();
}
return null;
}
}
let stack1 = new Stack<number>([]);
stack1.push(3);
stack1.push(4);
console.log(stack1.pop()); // 4
stack1.push(5);
stack1.push(7);
console.log(stack1.pop()); // 7
// If you wanted the stack to have mixed type of content you can do this:
// let stack1 = new Stack<any>([]);