forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_queue.ts
More file actions
87 lines (77 loc) · 2.11 KB
/
linked_queue.ts
File metadata and controls
87 lines (77 loc) · 2.11 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
import { Queue } from './queue'
type Node<T> = {
value: T
next?: Node<T>
}
/**
* This is a LinkedList-like implementation of a Queue,
* allowing the operations to be implemented in constant time.
* A Queue is a data structure that follows the FIFO (First-In First-Out) principle:
* The first element that was added to the queue will be the first one to be removed.
*/
export class LinkedQueue<T> implements Queue<T> {
public size: number
public head?: Node<T>
private tail?: Node<T>
constructor() {
this.head = this.tail = undefined
this.size = 0
}
/**
* Adds an item to the queue.
*
* @param item The item being added to the queue.
*/
enqueue(item: T): void {
const node = { value: item } as Node<T> // Creates a new node
this.size++ // Increase the length of the Queue
if (!this.tail) {
this.tail = this.head = node
return
}
this.tail.next = node // Updates the next tail to the node created
this.tail = node // The tail of the Queue then becomes the node created!!
}
/**
* Removes an item from the queue and returns it.
*
* @throws Queue Underflow if the queue is empty.
* @returns The item that was removed from the queue.
*/
dequeue(): T | undefined {
if (!this.head) {
throw new Error('Queue Underflow')
}
this.size--
const head = this.head // We store the head in order not to lose track of it
this.head = this.head.next // Update the the head to the next node
return head.value // Return the value of the head
}
/**
* Returns the item at the front of the queue.
*
* @returns The item at the front of the queue or null if the queue is empty.
*/
peek(): T | undefined | null {
if (this.isEmpty()) {
return null
}
return this.head?.value
}
/**
* Checks if the queue is empty.
*
* @returns {boolean} Whether the queue is empty or not.
*/
isEmpty(): boolean {
return this.size === 0
}
/**
* Returns the number of items in the queue.
*
* @returns {number} The number of items in the queue.
*/
length(): number {
return this.size
}
}