-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-array.ts
More file actions
78 lines (68 loc) · 1.67 KB
/
Copy pathstack-array.ts
File metadata and controls
78 lines (68 loc) · 1.67 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
/**
* Stack implementation using array with LIFO (Last In, First Out) behavior
*/
export class StackArray {
private items: number[] = [];
/**
* Creates a new stack with specified maximum length
* Stack is initialized with indices [0, 1, 2, ..., maxLength-1]
* @param maxLength - Maximum capacity of the stack
*/
constructor(private maxLength: number) {
this.items = Array.from({ length: maxLength }).map((_, index) => index);
}
/**
* Adds an item to the top of the stack (LIFO)
* @param item - The value to add to the stack
*/
public add(item: number): void {
this.items.push(item);
}
/**
* Removes and returns the item from the top of the stack (LIFO)
* @returns The last item added, or null if stack is empty
*/
public pop(): number | null {
if (this.size() === 0) {
return null;
}
return this.items.pop() ?? null;
}
/**
* Returns the item at the top of the stack without removing it
* @returns The last item added, or null if stack is empty
*/
public peek(): number | null {
if (this.size() === 0) {
return null;
}
return this.items[this.size() - 1];
}
/**
* Returns the current number of items in the stack
* @returns The stack size
*/
public size(): number {
return this.items.length;
}
/**
* Checks if the stack is empty
* @returns true if stack has no items, false otherwise
*/
public isEmpty(): boolean {
return this.size() === 0;
}
/**
* Checks if the stack is full
* @returns true if stack has reached maxLength, false otherwise
*/
public isFull(): boolean {
return this.size() >= this.maxLength;
}
/**
* Removes all items from the stack
*/
public clear(): void {
this.items = [];
}
}