-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.ts
More file actions
126 lines (107 loc) · 2.69 KB
/
stack.ts
File metadata and controls
126 lines (107 loc) · 2.69 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Arrays or Linked lists are both good implementations for a Stack. It depends on the operations (if more resize is needed).
// Array: Use less memory, but resize can take O(n). It is more cache Friendly because of spatial locality.
// Linked lists: use more Memory, but if they need to grow and shrink frequently dont need resizing operations.
import chalk from "chalk";
class Node<T> {
value: T;
next: Node<T> | null;
constructor(value: T) {
this.value = value;
this.next = null;
}
}
class StackLL<T> {
top: Node<T> | null;
bottom: Node<T> | null;
length: number;
constructor() {
this.top = null;
this.bottom = null;
this.length = 0;
}
peek(): T | null {
if (this.top) return this.top.value;
else return null;
}
push(value: T): T {
const node = new Node(value);
if (!this.top) {
this.top = node;
this.bottom = node;
this.length++;
return value;
}
node.next = this.top;
this.top = node;
this.length++;
return value;
}
pop(): null | T {
if (this.isEmpty()) {
return null;
}
const poppedValue = this.top!.value;
if (this.length === 1) {
this.top = null;
this.bottom = null;
this.length = 0;
return poppedValue;
} else {
this.top = this.top!.next;
this.length--;
return poppedValue;
}
}
isEmpty(): boolean {
return this.length === 0;
}
}
const stack = new StackLL();
console.log(chalk.blue("______STACK LINKED LIST_______"));
stack.push("Google");
console.log(chalk.blue(stack.peek())); // Google
stack.push("Amazon");
stack.push("Meta");
console.log(chalk.blue(stack.peek())); // Meta
for (let i = 0; i < 3; i++) {
console.log(chalk.blue(i + 1 + ": ", stack.pop()));
}
console.log(chalk.blue(stack.peek())); // null
/// ARRAY Implementation
class StackArray<T> {
stack: T[];
constructor() {
this.stack = [];
}
peek(): T | null {
if (this.stack.length) {
return this.stack[this.stack.length - 1];
} else {
return null;
}
}
push(value: T): T {
this.stack.push(value);
return value;
}
pop(): null | T {
if (this.isEmpty()) {
return null;
}
return this.stack.pop() as T;
}
isEmpty(): boolean {
return this.stack.length === 0;
}
}
const stackArr = new StackArray();
console.log(chalk.green("_________STACK ARRAY__________"));
stackArr.push("Arr Google");
console.log(chalk.green(stackArr.peek())); // Arr Google
stackArr.push("Arr Amazon");
stackArr.push("Arr Meta");
console.log(chalk.green(stackArr.peek())); // Arr Meta
for (let i = 0; i < 3; i++) {
console.log(chalk.green(i + 1 + ": ", stackArr.pop()));
}
console.log(chalk.green(stackArr.peek())); // null