Skip to content

Commit c83806f

Browse files
committed
fix: duplication of queue interface and minor bug
1 parent 1d6510a commit c83806f

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

data_structures/array_queue.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
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-
interface Queue<T> {
8-
enqueue(item: T): void
9-
dequeue(): T | undefined
10-
peek(): T | undefined | null
11-
isEmpty(): boolean
12-
13-
}
7+
import { Queue } from './queue'
148
export class ArrayQueue<T> implements Queue<T>{
159
private queue: T[] = [];
1610

data_structures/linkedlist_queue.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
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-
}
17+
import { Queue } from "./queue";
2418

2519
type Node<T> = {
2620
value: T,
@@ -32,19 +26,19 @@ type Node<T> = {
3226

3327
export class LinkedQueue<T> implements Queue<T> {
3428

35-
public length: number;
29+
public size: number;
3630
public head?: Node<T>;
3731
private tail?: Node<T>;
3832

3933
constructor() {
4034
this.head = this.tail = undefined;
41-
this.length = 0;
35+
this.size = 0;
4236
}
4337

4438
// adds elements to the rear/tail of the Queue
4539
enqueue(item: T): void {
4640
const node = { value: item } as Node<T>; // Creates a new node
47-
this.length++ // Increase the length of the Queue
41+
this.size++ // Increase the length of the Queue
4842

4943

5044
if (!this.tail) {
@@ -55,15 +49,17 @@ export class LinkedQueue<T> implements Queue<T> {
5549
this.tail = node; // The tail of the Queue then becomes the node created!!
5650

5751
}
52+
53+
5854
// Remove elements to the front/head of the Queue
5955
dequeue(): T | undefined {
6056

6157
// If there is no head return undefined
6258
if (!this.head) {
63-
return undefined;
59+
throw new Error("Queue Underflow");
6460
}
6561

66-
this.length--;
62+
this.size--;
6763
let head = this.head; // We store the head in order not to lose track of it
6864
this.head = this.head.next; // Update the the head to the next node
6965
return head.value; // Return the value of the head
@@ -77,7 +73,11 @@ export class LinkedQueue<T> implements Queue<T> {
7773

7874
// Returns true if the Queue is empty
7975
isEmpty(): boolean {
80-
return this.length == 0
76+
return this.size == 0
77+
}
78+
79+
length(): number {
80+
return this.size;
8181
}
8282
}
8383

data_structures/queue.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface Queue<T> {
2+
enqueue(item: T): void
3+
dequeue(): T | undefined
4+
peek(): T | undefined | null
5+
isEmpty(): boolean
6+
length(): number
7+
8+
}

data_structures/test/linkedlist_queue.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("Testing Queue data structure", () => {
66
const queue = new LinkedQueue<number>();
77
queue.enqueue(1);
88

9-
expect(queue.length).toBe(1);
9+
expect(queue.length()).toBe(1);
1010
});
1111

1212
it("isEmpty should return true on empty queue", () => {
@@ -21,26 +21,41 @@ describe("Testing Queue data structure", () => {
2121
expect(queue.isEmpty()).toBeFalsy();
2222
});
2323

24+
it("front should return the first value", () => {
25+
const queue = new LinkedQueue<number>();
26+
queue.enqueue(1);
27+
28+
expect(queue.peek()).toBe(1);
29+
});
30+
31+
it("front should return null when the queue is empty", () => {
32+
const queue = new LinkedQueue<number>();
2433

34+
expect(queue.peek()).toBe(undefined);
35+
});
2536

2637
it("length should return the number of elements in the queue", () => {
2738
const queue = new LinkedQueue<number>();
2839
queue.enqueue(1);
2940
queue.enqueue(1);
3041
queue.enqueue(1);
3142

32-
expect(queue.length).toBe(3);
43+
expect(queue.length()).toBe(3);
3344
});
3445

3546
it("dequeue should remove the first element", () => {
3647
const queue = new LinkedQueue<number>();
3748
queue.enqueue(1);
3849
queue.enqueue(2);
3950
queue.enqueue(3);
51+
queue.dequeue();
4052

41-
42-
expect(queue.length).toBe(3);
53+
expect(queue.length()).toBe(2);
4354
});
4455

56+
it("dequeue should throw error on empty queue", () => {
57+
const queue = new LinkedQueue<number>();
4558

59+
expect(() => queue.dequeue()).toThrow("Queue Underflow");
60+
});
4661
});

0 commit comments

Comments
 (0)