-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1-queue.ts
47 lines (39 loc) · 1.01 KB
/
1-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
export class Queue<T> {
private items: T[];
constructor() {
this.items = [];
}
// Step 2: Enqueue method
enqueue(element: T): void {
this.items.push(element);
}
// Step 3: Dequeue method
dequeue(): T | null {
return this.items.length === 0 ? null : this.items.shift() || null;
}
// Step 4: Front method
front(): T | null {
return this.items.length === 0 ? null : this.items[0];
}
// Step 5: isEmpty and size methods
isEmpty(): boolean {
return this.items.length === 0;
}
size(): number {
return this.items.length;
}
// Step 6: Print method
print(): void {
console.log(this.items.join(" <- "));
}
}
// Uncomment The Code Below to See If It Works!
const queue = new Queue<number>(); // Create a queue that stores numbers
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.print(); // Output: 10 <- 20 <- 30
console.log(queue.dequeue()); // 10
console.log(queue.front()); // 20
console.log(queue.size()); // 2
console.log(queue.isEmpty()); // false