-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.ts
More file actions
39 lines (31 loc) · 687 Bytes
/
Stack.ts
File metadata and controls
39 lines (31 loc) · 687 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
37
38
39
export class StackNode<T> {
next: StackNode<T> | null = null;
constructor(public value: T) {}
}
export class Stack<T> {
private _first: StackNode<T> | null = null;
private _last: StackNode<T> | null = null;
private _length = 0;
get first() {
return this._first;
}
get last() {
return this._last;
}
get length() {
return this._length;
}
push(value: T): number {
const node = new StackNode(value);
if (!this._first) {
this._first = node;
this._last = node;
} else {
const currentFirst = this._first;
this._first = node;
node.next = currentFirst;
}
this._length++;
return this._length;
}
}