Skip to content

Commit bce42a7

Browse files
committed
fix: added the queue interface
1 parent c41f34b commit bce42a7

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

data_structures/array_queue.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
* It means that the first element that was added to the queue will be the first one to be removed.
55
* The time complexity of the operations is O(n).
66
*/
7-
export class ArrayQueue<T> {
7+
interface Queue<T> {
8+
enqueue(item: T): void
9+
dequeue(): T | undefined
10+
peek(): T | undefined | null
11+
isEmpty(): boolean
12+
13+
}
14+
export class ArrayQueue<T> implements Queue<T>{
815
private queue: T[] = [];
916

1017
/**
@@ -53,7 +60,7 @@ export class ArrayQueue<T> {
5360
*
5461
* @returns The item at the front of the queue or null if the queue is empty.
5562
*/
56-
front(): T | null {
63+
peek(): T | null {
5764
if (this.isEmpty()) {
5865
return null;
5966
}

data_structures/linkedlist_queue.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414
1515
*/
1616

17+
export interface Queue<T> {
18+
enqueue(item: T): void
19+
dequeue(): T | undefined
20+
peek(): T | undefined | null
21+
isEmpty(): boolean
22+
23+
}
24+
1725
type Node<T> = {
1826
value: T,
1927
next?: Node<T>,
2028
}
2129

2230

23-
export class LinkedQueue<T> {
31+
32+
33+
export class LinkedQueue<T> implements Queue<T> {
2434

2535
public length: number;
2636
public head?: Node<T>;
@@ -46,7 +56,7 @@ export class LinkedQueue<T> {
4656

4757
}
4858
// Remove elements to the front/head of the Queue
49-
deque(): T | undefined {
59+
dequeue(): T | undefined {
5060

5161
// If there is no head return undefined
5262
if (!this.head) {

data_structures/test/array_queue.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ describe("Testing Queue data structure", () => {
2424
const queue = new ArrayQueue<number>();
2525
queue.enqueue(1);
2626

27-
expect(queue.front()).toBe(1);
27+
expect(queue.peek()).toBe(1);
2828
});
2929

3030
it("front should return null when the queue is empty", () => {
3131
const queue = new ArrayQueue<number>();
3232

33-
expect(queue.front()).toBe(null);
33+
expect(queue.peek()).toBe(null);
3434
});
3535

3636
it("length should return the number of elements in the queue", () => {

0 commit comments

Comments
 (0)