Skip to content

Commit 55cd754

Browse files
authored
structure: queue (TheAlgorithms#84)
* feat: implements the data structure queue * fix: resolves requested changes - The removed element is now returned in `dequeue()` - Files have been renamed - Time complexity note has been added - Adjusted tests accordingly
1 parent 6f3ea82 commit 55cd754

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

data_structures/array_queue.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* This is an array-based implementation of a Queue.
3+
* A Queue is a data structure that follows the FIFO (First In First Out) principle.
4+
* It means that the first element that was added to the queue will be the first one to be removed.
5+
* The time complexity of the operations is O(n).
6+
*/
7+
export class ArrayQueue<T> {
8+
private queue: T[] = [];
9+
10+
/**
11+
* Returns the number of items in the queue.
12+
*
13+
* @returns {number} The number of items in the queue.
14+
*/
15+
length(): number {
16+
return this.queue.length;
17+
}
18+
19+
/**
20+
* Checks if the queue is empty.
21+
*
22+
* @returns {boolean} Whether the queue is empty or not.
23+
*/
24+
isEmpty(): boolean {
25+
return this.queue.length === 0;
26+
}
27+
28+
/**
29+
* Adds an item to the queue.
30+
*
31+
* @param item The item being added to the queue.
32+
*/
33+
enqueue(item: T): void {
34+
this.queue.push(item);
35+
}
36+
37+
/**
38+
* Removes an item from the queue and returns it.
39+
*
40+
* @throws Queue Underflow if the queue is empty.
41+
* @returns The item that was removed from the queue.
42+
*/
43+
dequeue(): T {
44+
if (this.isEmpty()) {
45+
throw new Error("Queue Underflow");
46+
}
47+
48+
return this.queue.shift() as T;
49+
}
50+
51+
/**
52+
* Returns the item at the front of the queue.
53+
*
54+
* @returns The item at the front of the queue or null if the queue is empty.
55+
*/
56+
front(): T | null {
57+
if (this.isEmpty()) {
58+
return null;
59+
}
60+
61+
return this.queue[0];
62+
}
63+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ArrayQueue } from "../array_queue";
2+
3+
describe("Testing Queue data structure", () => {
4+
it("enqueue should add a new element to the queue", () => {
5+
const queue = new ArrayQueue<number>();
6+
queue.enqueue(1);
7+
8+
expect(queue.length()).toBe(1);
9+
});
10+
11+
it("isEmpty should return true on empty queue", () => {
12+
const queue = new ArrayQueue<number>();
13+
expect(queue.isEmpty()).toBeTruthy();
14+
});
15+
16+
it("isEmpty should return false on not empty queue", () => {
17+
const queue = new ArrayQueue<number>();
18+
queue.enqueue(1);
19+
20+
expect(queue.isEmpty()).toBeFalsy();
21+
});
22+
23+
it("front should return the first value", () => {
24+
const queue = new ArrayQueue<number>();
25+
queue.enqueue(1);
26+
27+
expect(queue.front()).toBe(1);
28+
});
29+
30+
it("front should return null when the queue is empty", () => {
31+
const queue = new ArrayQueue<number>();
32+
33+
expect(queue.front()).toBe(null);
34+
});
35+
36+
it("length should return the number of elements in the queue", () => {
37+
const queue = new ArrayQueue<number>();
38+
queue.enqueue(1);
39+
queue.enqueue(1);
40+
queue.enqueue(1);
41+
42+
expect(queue.length()).toBe(3);
43+
});
44+
45+
it("dequeue should remove the first element", () => {
46+
const queue = new ArrayQueue<number>();
47+
queue.enqueue(1);
48+
queue.enqueue(2);
49+
queue.enqueue(3);
50+
queue.dequeue();
51+
52+
expect(queue.length()).toBe(2);
53+
});
54+
55+
it("dequeue should throw error on empty queue", () => {
56+
const queue = new ArrayQueue<number>();
57+
58+
expect(() => queue.dequeue()).toThrow("Queue Underflow");
59+
});
60+
});

0 commit comments

Comments
 (0)