-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.ts
More file actions
88 lines (80 loc) · 1.8 KB
/
Copy pathStack.ts
File metadata and controls
88 lines (80 loc) · 1.8 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
import ObservableMethods from "./ObservableMethod";
/**
* A class representing a stack data structure.
*
* @class Stack
* @template T - The type of items held in the stack.
*/
@ObservableMethods
export default class Stack<T = any> {
/**
* The internal array that holds the stack items.
*
* @private
* @type {T[]}
*/
private stack: any[] = [];
/**
* Creates an instance of Stack.
*
* @param {...T[]} args - Initial items to populate the stack.
*/
constructor(...args: T[]) {
this.stack = args;
this.pop = this.pop.bind(this);
this.peek = this.peek.bind(this);
this.isEmpty = this.isEmpty.bind(this);
this.size = this.size.bind(this);
this.clear = this.clear.bind(this);
this.push = this.push.bind(this);
}
/**
* Adds an item to the top of the stack.
*
* @param {T} arg - The item to add.
* @returns {number} The new length of the stack.
*/
push(arg: T) {
const length = this.stack.push(arg);
return length;
}
/**
* Removes and returns the top item of the stack.
*
* @returns {T} The removed item, or undefined if the stack is empty.
*/
pop(): T {
const item = this.stack.pop();
return item;
}
/**
* Returns the top item of the stack without removing it.
*
* @returns {T} The top item, or undefined if the stack is empty.
*/
peek(): T {
return this.stack[this.stack.length - 1];
}
/**
* Checks if the stack is empty.
*
* @returns {boolean} True if the stack is empty, false otherwise.
*/
isEmpty() {
return this.stack.length === 0;
}
/**
* Returns the number of items in the stack.
*
* @returns {number} The number of items in the stack.
*/
size() {
return this.stack.length;
}
/**
* Clear the stack.
*/
clear() {
this.stack = [];
}
}