-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Stack.ts
41 lines (35 loc) · 991 Bytes
/
Stack.ts
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
import { Queuelike } from "./interfaces";
import { SingleLinkedList } from "./SingleLinkedList";
/**
* A _LIFO queue_, where the last element to be pushed into the queue is the
* first element to be popped out of it.
*
* ```ts
* import { Stack } from "scl"
* ```
*
* Pushing and popping an element are both in `O(1)`.
*
* | Property name | Worst-case |
* |-------------------------------|------------|
* | {@link Stack.add add()} | O(1) |
* | {@link Stack.peek peek()} | O(1) |
* | {@link Stack.pop pop()} | O(1) |
* | {@link Stack.size size} | O(1) |
*
* @typeparam T The type of element in this queue.
*
* @see [[Queue]]
* @see [[PriorityQueue]]
*/
export class Stack<T> extends SingleLinkedList<T> implements Queuelike<T> {
public peek() {
return this.first();
}
public pop() {
const cursor = this.at(0);
this.deleteAt(cursor);
return cursor.value;
}
}
export default Stack;