-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.ts
55 lines (48 loc) · 1.3 KB
/
queue.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { LinkedList } from "@/data-structure/linked-list";
/**
* A queue is a linear data structure that follows the First In First Out (FIFO) principle.
*/
export class Queue<Item> {
public list: LinkedList<Item>;
constructor() {
this.list = new LinkedList<Item>();
}
/**
* Removes the item at the front of the queue and returns it.
* @returns the removed item or null if queue is empty.
*/
public dequeue(): Item | null {
const removed = this.list.deleteHead();
return removed ? removed.value : null;
}
/**
* Adds an item to the end of the queue.
* @param item to be added.
*/
public enqueue(item: Item) {
this.list.append(item);
}
/**
* Returns the item at front of the queue whitout removing it.
* @returns null or item.
*/
public peek(): Item | null {
if (this.isEmpty()) return null;
return this.list.head?.value ?? null;
}
/**
* Checks if queue is empty or not.
* @returns true or false.
*/
public isEmpty(): boolean {
return !this.list.head;
}
/**
* Returns comma separate string form the queue.
* @param callback optional callback function.
* @returns string form of the queue or empty string.
*/
public toString(callback?: (value: Item) => string): string {
return this.list.toString(callback);
}
}