Skip to content

feat: add Linked Queue #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8e48bbe
feat: add Array Queue to Directory.md
co-bby Jan 3, 2023
a0e5f53
feat: add linkedlist Queue
co-bby Jan 5, 2023
1495408
feat: add test for linkedlist queue
co-bby Jan 5, 2023
5d368a6
feat: update Directory.md
co-bby Jan 5, 2023
c41f34b
fixed typos and formatting
co-bby Jan 5, 2023
60f0ec0
BREAKING CHANGE: change front to peek
co-bby Jan 6, 2023
bce42a7
fix: added the queue interface
co-bby Jan 6, 2023
1d6510a
Merge branch 'master' of https://github.com/co-bby/TypeScript
co-bby Jan 6, 2023
c83806f
fix: duplication of queue interface and minor bug
co-bby Jan 6, 2023
dd12220
fix: queue test problem
co-bby Jan 6, 2023
e207606
fix: change filename to match naming convention
co-bby Jan 7, 2023
c65d5fb
fix: leading newline and time complexity
co-bby Jan 8, 2023
0703824
feat: add jsDoc comments
co-bby Jan 8, 2023
57ba509
Merge branch 'master' into myBranch
appgurueu Jan 8, 2023
feb549f
Rename Queue.test.ts to queue.test.ts
appgurueu Jan 8, 2023
e9a6a9d
Update queue.test.ts
appgurueu Jan 8, 2023
47e9e7e
Update array_queue.test.ts
appgurueu Jan 8, 2023
7d71faa
Update and rename linkedlist_queue.test.ts to linked_queue.test.ts
appgurueu Jan 8, 2023
ddf5c95
Update and rename linkedlist_queue.ts to linked_queue.ts
appgurueu Jan 8, 2023
b46be61
Update linked_queue.test.ts
appgurueu Jan 8, 2023
e0f9ca6
Update linked_queue.test.ts
appgurueu Jan 8, 2023
83b61d2
Rename queue.test.ts to queue.ts
appgurueu Jan 8, 2023
7f2e3fe
Update array_queue.test.ts
appgurueu Jan 8, 2023
2a7ce9c
Update linked_queue.test.ts
appgurueu Jan 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Data Structures
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
* [Array Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/array_queue.ts)
* [Linkedlist Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/linkedlist_queue.ts)

## Dynamic Programming
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
Expand Down
5 changes: 3 additions & 2 deletions data_structures/array_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* It means that the first element that was added to the queue will be the first one to be removed.
* The time complexity of the operations is O(n).
*/
export class ArrayQueue<T> {
import { Queue } from './queue'
export class ArrayQueue<T> implements Queue<T>{
private queue: T[] = [];

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ export class ArrayQueue<T> {
*
* @returns The item at the front of the queue or null if the queue is empty.
*/
front(): T | null {
peek(): T | null {
if (this.isEmpty()) {
return null;
}
Expand Down
84 changes: 84 additions & 0 deletions data_structures/linkedlist_queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* This is an linkedlist-based implementation of a Queue.
* A Queue is a data structure that follows the FIFO (First In First Out) principle.
* It means that the first element that was added to the queue will be the first one to be removed.
* The time complexity of the operations is O(n).

*/

/** USAGE

*Printers
*CPU task scheduling
*Callback queue in javascript

*/

import { Queue } from "./queue";

type Node<T> = {
value: T,
next?: Node<T>,
}

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

public size: number;
public head?: Node<T>;
private tail?: Node<T>;

constructor() {
this.head = this.tail = undefined;
this.size = 0;
}

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


if (!this.tail) {
this.tail = this.head = node;
return;
}
this.tail.next = node; // Updates the next tail to the node created
this.tail = node; // The tail of the Queue then becomes the node created!!

}


// Remove elements to the front/head of the Queue
dequeue(): T | undefined {

// If there is no head return undefined
if (!this.head) {
throw new Error("Queue Underflow");
}

this.size--;
let head = this.head; // We store the head in order not to lose track of it
this.head = this.head.next; // Update the the head to the next node
return head.value; // Return the value of the head
}


// Returns the value of the head
peek(): T | undefined | null {

if (this.isEmpty()) {
return null;
}
return this.head?.value;
}

// Returns true if the Queue is empty
isEmpty(): boolean {
return this.size === 0
}

length(): number {
return this.size;
}
}

8 changes: 8 additions & 0 deletions data_structures/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Queue<T> {
enqueue(item: T): void
dequeue(): T | undefined
peek(): T | undefined | null
isEmpty(): boolean
length(): number

}
64 changes: 64 additions & 0 deletions data_structures/test/Queue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

import { Queue } from '../queue';
type QueueConstructor = new <T>() => Queue<T>
export function test(Queue: QueueConstructor) {
describe("Testing Queue data structure", () => {
it("enqueue should add a new element to the queue", () => {
const queue = new Queue<number>();
queue.enqueue(1);

expect(queue.length()).toBe(1);
});

it("isEmpty should return true on empty queue", () => {
const queue = new Queue<number>();
expect(queue.isEmpty()).toBeTruthy();
});

it("isEmpty should return false on not empty queue", () => {
const queue = new Queue<number>();
queue.enqueue(1);

expect(queue.isEmpty()).toBeFalsy();
});

it("front should return the first value", () => {
const queue = new Queue<number>();
queue.enqueue(1);

expect(queue.peek()).toBe(1);
});

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

expect(queue.peek()).toBe(null);
});

it("length should return the number of elements in the queue", () => {
const queue = new Queue<number>();
queue.enqueue(1);
queue.enqueue(1);
queue.enqueue(1);

expect(queue.length()).toBe(3);
});

it("dequeue should remove the first element", () => {
const queue = new Queue<number>();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.dequeue();

expect(queue.length()).toBe(2);
});

it("dequeue should throw error on empty queue", () => {
const queue = new Queue<number>();

expect(() => queue.dequeue()).toThrow("Queue Underflow");
});
});

}
57 changes: 2 additions & 55 deletions data_structures/test/array_queue.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,7 @@
import { ArrayQueue } from "../array_queue";

describe("Testing Queue data structure", () => {
it("enqueue should add a new element to the queue", () => {
const queue = new ArrayQueue<number>();
queue.enqueue(1);
import { test } from './Queue.test'

expect(queue.length()).toBe(1);
});
test(ArrayQueue);

it("isEmpty should return true on empty queue", () => {
const queue = new ArrayQueue<number>();
expect(queue.isEmpty()).toBeTruthy();
});

it("isEmpty should return false on not empty queue", () => {
const queue = new ArrayQueue<number>();
queue.enqueue(1);

expect(queue.isEmpty()).toBeFalsy();
});

it("front should return the first value", () => {
const queue = new ArrayQueue<number>();
queue.enqueue(1);

expect(queue.front()).toBe(1);
});

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

expect(queue.front()).toBe(null);
});

it("length should return the number of elements in the queue", () => {
const queue = new ArrayQueue<number>();
queue.enqueue(1);
queue.enqueue(1);
queue.enqueue(1);

expect(queue.length()).toBe(3);
});

it("dequeue should remove the first element", () => {
const queue = new ArrayQueue<number>();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.dequeue();

expect(queue.length()).toBe(2);
});

it("dequeue should throw error on empty queue", () => {
const queue = new ArrayQueue<number>();

expect(() => queue.dequeue()).toThrow("Queue Underflow");
});
});
6 changes: 6 additions & 0 deletions data_structures/test/linkedlist_queue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from './Queue.test'

import { LinkedQueue } from '../linkedlist_queue';

test(LinkedQueue);