-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.ts
116 lines (86 loc) · 2.88 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Node from '../Linked-Lists/SinglyNode.ts';
export default class Queue<T> {
private last: Node<T> | null;
private first: Node<T>| null;
private length: number;
constructor() {
this.last = null;
this.first = null;
this.length = 0;
}
public getLength(): number {
return this.length;
}
public isEmpty(): boolean {
return this.length === 0;
}
public peek(): T | null {
return this.first?.getValue() || null;
}
public enqueue(value: T): boolean {
const newNode = new Node(value);
if (!this.last) { // If empty queue, initialize
this.first = newNode;
this.last = newNode;
}
else {
this.last.setNext(newNode);
this.last = this.last.getNext();
}
++this.length;
return true;
}
public dequeue(): T | any {
if (!this.first) return null; // Edge case: Empty queue
if (this.length === 1) { // Edge case: Queue has 1 element, so a dequeue should reset the queue's state
this.last = null; // Challenge: What is the state of each 'class field' after a dequeue when 1 element was remaining?
}
const value = this.first.getValue();
this.first = this.first.getNext();
--this.length;
return value;
}
}
function printQueue(queue: Queue<any>) {
console.log(JSON.stringify(queue));
}
function printPeekQueue(queue: Queue<any>) {
console.log('Peeking... Found', JSON.stringify(queue.peek()));
}
function printDequeue(queue: Queue<any>) {
console.log('Dequeued:', JSON.stringify(queue.dequeue()));
}
//---------------------------------------------------------------------
// ---------- MAIN PROGRAM ----------
//---------------------------------------------------------------------
if (import.meta.main) {
const ATLA = new Queue<string>();
printPeekQueue(ATLA);
ATLA.enqueue('Sokka');
ATLA.enqueue('Katara');
printPeekQueue(ATLA);
ATLA.enqueue('Aang');
ATLA.enqueue('Appa');
printQueue(ATLA);
printDequeue(ATLA);
printDequeue(ATLA);
printDequeue(ATLA);
printQueue(ATLA);
printDequeue(ATLA);
printQueue(ATLA);
ATLA.enqueue('Zuko');
ATLA.enqueue('Iroh');
printQueue(ATLA);
// RUN: deno run Data-Structures/Sequential/Queue.ts
}
// --------------------------- Terminal Output: ---------------------------
// Peeking... Found null
// Peeking... Found "Sokka"
// {"last":{"value":"Appa","next":null},"first":{"value":"Sokka","next":{"value":"Katara","next":{"value":"Aang","next":{"value":"Appa","next":null}}}},"length":4}
// Dequeued: "Sokka"
// Dequeued: "Katara"
// Dequeued: "Aang"
// {"last":{"value":"Appa","next":null},"first":{"value":"Appa","next":null},"length":1}
// Dequeued: "Appa"
// {"last":null,"first":null,"length":0}
// {"last":{"value":"Iroh","next":null},"first":{"value":"Zuko","next":{"value":"Iroh","next":null}},"length":2}