From 8e48bbe90dcfabd1ee76fb1c69f304b8e515fc2c Mon Sep 17 00:00:00 2001 From: co-bby Date: Tue, 3 Jan 2023 17:26:31 -0500 Subject: [PATCH 01/22] feat: add Array Queue to Directory.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 71d04f08..2e003e91 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -4,6 +4,7 @@ ## 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) ## Dynamic Programming * [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts) From a0e5f531b1110c30d711b6917d6494a9104de9d7 Mon Sep 17 00:00:00 2001 From: co-bby Date: Thu, 5 Jan 2023 12:48:16 -0500 Subject: [PATCH 02/22] feat: add linkedlist Queue --- data_structures/linkedlist_queue.ts | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 data_structures/linkedlist_queue.ts diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts new file mode 100644 index 00000000..44d72cb5 --- /dev/null +++ b/data_structures/linkedlist_queue.ts @@ -0,0 +1,65 @@ +/* The queue data strcture is sequential collection of elements that +follows the priciple of FIFO */ +/*here we are using a singly linkedlist so we are only going to traverse to the next node */ + +/*USAGE +*Printers +*CPU task scheduling +*Callback queue in javascript + +*/ + +type Node = { + value: T, + next?: Node, +} + + +export class LinkedlistQueue { + + public length: number; + public head?: Node; + private tail?: Node; + + constructor() { + this.head = this.tail = undefined; //when a new linkedlist is created the head becomes equal to the tail . + this.length = 0; + } + + // adds elements to the rear/tail of the collection. + enqueue(item: T): void { + const node = { value: item } as Node; //creates a new node + this.length++ //increase the length of the linkedlist + + + 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 linkedlist then becomes the node created!! + + } + //remove elements to the front/head of the collection + deque(): T | undefined { + if (!this.head) { + return undefined; //if there is no head return undefined + } + this.length--; + 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 { + return this.head?.value; + } + + // checks if the the linkedlist is empty + isEmpty(): boolean { + return this.length == 0 + } +} + From 1495408c845955a2317691802aeab49f581a28f4 Mon Sep 17 00:00:00 2001 From: co-bby Date: Thu, 5 Jan 2023 12:49:06 -0500 Subject: [PATCH 03/22] feat: add test for linkedlist queue --- data_structures/test/linkedlist_queue.test.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 data_structures/test/linkedlist_queue.test.ts diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts new file mode 100644 index 00000000..a1d8426e --- /dev/null +++ b/data_structures/test/linkedlist_queue.test.ts @@ -0,0 +1,46 @@ + +import { LinkedlistQueue } from './../linkedlist_queue'; + +describe("Testing Queue data structure", () => { + it("enqueue should add a new element to the queue", () => { + const queue = new LinkedlistQueue(); + queue.enqueue(1); + + expect(queue.length).toBe(1); + }); + + it("isEmpty should return true on empty queue", () => { + const queue = new LinkedlistQueue(); + expect(queue.isEmpty()).toBeTruthy(); + }); + + it("isEmpty should return false on not empty queue", () => { + const queue = new LinkedlistQueue(); + queue.enqueue(1); + + expect(queue.isEmpty()).toBeFalsy(); + }); + + + + it("length should return the number of elements in the queue", () => { + const queue = new LinkedlistQueue(); + queue.enqueue(1); + queue.enqueue(1); + queue.enqueue(1); + + expect(queue.length).toBe(3); + }); + + it("dequeue should remove the first element", () => { + const queue = new LinkedlistQueue(); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + + + expect(queue.length).toBe(3); + }); + + +}); From 5d368a689a1440da901cecc9bef50952bb5c15c3 Mon Sep 17 00:00:00 2001 From: co-bby Date: Thu, 5 Jan 2023 13:06:46 -0500 Subject: [PATCH 04/22] feat: update Directory.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2e003e91..b96d789d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -5,6 +5,7 @@ ## 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) From c41f34b0d1035dcb755e89e5cee980a80903485a Mon Sep 17 00:00:00 2001 From: co-bby Date: Thu, 5 Jan 2023 17:11:15 -0500 Subject: [PATCH 05/22] fixed typos and formatting --- data_structures/linkedlist_queue.ts | 44 +++++++++++-------- data_structures/test/linkedlist_queue.test.ts | 12 ++--- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts index 44d72cb5..bbba5530 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linkedlist_queue.ts @@ -1,8 +1,13 @@ -/* The queue data strcture is sequential collection of elements that -follows the priciple of FIFO */ -/*here we are using a singly linkedlist so we are only going to traverse to the next node */ +/** + * 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 -/*USAGE *Printers *CPU task scheduling *Callback queue in javascript @@ -15,49 +20,52 @@ type Node = { } -export class LinkedlistQueue { +export class LinkedQueue { public length: number; public head?: Node; private tail?: Node; constructor() { - this.head = this.tail = undefined; //when a new linkedlist is created the head becomes equal to the tail . + this.head = this.tail = undefined; this.length = 0; } - // adds elements to the rear/tail of the collection. + // adds elements to the rear/tail of the Queue enqueue(item: T): void { - const node = { value: item } as Node; //creates a new node - this.length++ //increase the length of the linkedlist + const node = { value: item } as Node; // Creates a new node + this.length++ // 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 linkedlist then becomes the node created!! + 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 collection + // Remove elements to the front/head of the Queue deque(): T | undefined { + + // If there is no head return undefined if (!this.head) { - return undefined; //if there is no head return undefined + return undefined; } + this.length--; - 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 + 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 + // Returns the value of the head peek(): T | undefined { return this.head?.value; } - // checks if the the linkedlist is empty + // Returns true if the Queue is empty isEmpty(): boolean { return this.length == 0 } diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts index a1d8426e..9ef05c58 100644 --- a/data_structures/test/linkedlist_queue.test.ts +++ b/data_structures/test/linkedlist_queue.test.ts @@ -1,21 +1,21 @@ -import { LinkedlistQueue } from './../linkedlist_queue'; +import { LinkedQueue } from './../linkedlist_queue'; describe("Testing Queue data structure", () => { it("enqueue should add a new element to the queue", () => { - const queue = new LinkedlistQueue(); + const queue = new LinkedQueue(); queue.enqueue(1); expect(queue.length).toBe(1); }); it("isEmpty should return true on empty queue", () => { - const queue = new LinkedlistQueue(); + const queue = new LinkedQueue(); expect(queue.isEmpty()).toBeTruthy(); }); it("isEmpty should return false on not empty queue", () => { - const queue = new LinkedlistQueue(); + const queue = new LinkedQueue(); queue.enqueue(1); expect(queue.isEmpty()).toBeFalsy(); @@ -24,7 +24,7 @@ describe("Testing Queue data structure", () => { it("length should return the number of elements in the queue", () => { - const queue = new LinkedlistQueue(); + const queue = new LinkedQueue(); queue.enqueue(1); queue.enqueue(1); queue.enqueue(1); @@ -33,7 +33,7 @@ describe("Testing Queue data structure", () => { }); it("dequeue should remove the first element", () => { - const queue = new LinkedlistQueue(); + const queue = new LinkedQueue(); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); From 60f0ec0732776e92b8ce64a296181e051477d822 Mon Sep 17 00:00:00 2001 From: co-bby Date: Fri, 6 Jan 2023 11:45:20 -0500 Subject: [PATCH 06/22] BREAKING CHANGE: change front to peek --- data_structures/array_queue.ts | 2 +- data_structures/test/array_queue.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/array_queue.ts b/data_structures/array_queue.ts index 242c64d3..19cb0492 100644 --- a/data_structures/array_queue.ts +++ b/data_structures/array_queue.ts @@ -53,7 +53,7 @@ export class ArrayQueue { * * @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; } diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index 34c74fd5..ffa87206 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -24,13 +24,13 @@ describe("Testing Queue data structure", () => { const queue = new ArrayQueue(); queue.enqueue(1); - expect(queue.front()).toBe(1); + expect(queue.peek()).toBe(1); }); it("front should return null when the queue is empty", () => { const queue = new ArrayQueue(); - expect(queue.front()).toBe(null); + expect(queue.peek()).toBe(null); }); it("length should return the number of elements in the queue", () => { From bce42a70687d2ddcaf7c131848c37450a54e9b3e Mon Sep 17 00:00:00 2001 From: co-bby Date: Fri, 6 Jan 2023 12:12:43 -0500 Subject: [PATCH 07/22] fix: added the queue interface --- data_structures/array_queue.ts | 11 +++++++++-- data_structures/linkedlist_queue.ts | 14 ++++++++++++-- data_structures/test/array_queue.test.ts | 4 ++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/data_structures/array_queue.ts b/data_structures/array_queue.ts index 242c64d3..f9d66dad 100644 --- a/data_structures/array_queue.ts +++ b/data_structures/array_queue.ts @@ -4,7 +4,14 @@ * 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 { +interface Queue { + enqueue(item: T): void + dequeue(): T | undefined + peek(): T | undefined | null + isEmpty(): boolean + +} +export class ArrayQueue implements Queue{ private queue: T[] = []; /** @@ -53,7 +60,7 @@ export class ArrayQueue { * * @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; } diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts index bbba5530..04b055f2 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linkedlist_queue.ts @@ -14,13 +14,23 @@ */ +export interface Queue { + enqueue(item: T): void + dequeue(): T | undefined + peek(): T | undefined | null + isEmpty(): boolean + +} + type Node = { value: T, next?: Node, } -export class LinkedQueue { + + +export class LinkedQueue implements Queue { public length: number; public head?: Node; @@ -46,7 +56,7 @@ export class LinkedQueue { } // Remove elements to the front/head of the Queue - deque(): T | undefined { + dequeue(): T | undefined { // If there is no head return undefined if (!this.head) { diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index 34c74fd5..ffa87206 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -24,13 +24,13 @@ describe("Testing Queue data structure", () => { const queue = new ArrayQueue(); queue.enqueue(1); - expect(queue.front()).toBe(1); + expect(queue.peek()).toBe(1); }); it("front should return null when the queue is empty", () => { const queue = new ArrayQueue(); - expect(queue.front()).toBe(null); + expect(queue.peek()).toBe(null); }); it("length should return the number of elements in the queue", () => { From c83806fb7b12ddfbe9ae9deec713901041414ab8 Mon Sep 17 00:00:00 2001 From: co-bby Date: Fri, 6 Jan 2023 12:37:05 -0500 Subject: [PATCH 08/22] fix: duplication of queue interface and minor bug --- data_structures/array_queue.ts | 8 +----- data_structures/linkedlist_queue.ts | 26 +++++++++---------- data_structures/queue.ts | 8 ++++++ data_structures/test/linkedlist_queue.test.ts | 23 +++++++++++++--- 4 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 data_structures/queue.ts diff --git a/data_structures/array_queue.ts b/data_structures/array_queue.ts index f9d66dad..19027768 100644 --- a/data_structures/array_queue.ts +++ b/data_structures/array_queue.ts @@ -4,13 +4,7 @@ * 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). */ -interface Queue { - enqueue(item: T): void - dequeue(): T | undefined - peek(): T | undefined | null - isEmpty(): boolean - -} +import { Queue } from './queue' export class ArrayQueue implements Queue{ private queue: T[] = []; diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts index 04b055f2..2e103c9d 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linkedlist_queue.ts @@ -14,13 +14,7 @@ */ -export interface Queue { - enqueue(item: T): void - dequeue(): T | undefined - peek(): T | undefined | null - isEmpty(): boolean - -} +import { Queue } from "./queue"; type Node = { value: T, @@ -32,19 +26,19 @@ type Node = { export class LinkedQueue implements Queue { - public length: number; + public size: number; public head?: Node; private tail?: Node; constructor() { this.head = this.tail = undefined; - this.length = 0; + this.size = 0; } // adds elements to the rear/tail of the Queue enqueue(item: T): void { const node = { value: item } as Node; // Creates a new node - this.length++ // Increase the length of the Queue + this.size++ // Increase the length of the Queue if (!this.tail) { @@ -55,15 +49,17 @@ export class LinkedQueue implements Queue { 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) { - return undefined; + throw new Error("Queue Underflow"); } - this.length--; + 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 @@ -77,7 +73,11 @@ export class LinkedQueue implements Queue { // Returns true if the Queue is empty isEmpty(): boolean { - return this.length == 0 + return this.size == 0 + } + + length(): number { + return this.size; } } diff --git a/data_structures/queue.ts b/data_structures/queue.ts new file mode 100644 index 00000000..bac8c07c --- /dev/null +++ b/data_structures/queue.ts @@ -0,0 +1,8 @@ +export interface Queue { + enqueue(item: T): void + dequeue(): T | undefined + peek(): T | undefined | null + isEmpty(): boolean + length(): number + +} \ No newline at end of file diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts index 9ef05c58..b182c250 100644 --- a/data_structures/test/linkedlist_queue.test.ts +++ b/data_structures/test/linkedlist_queue.test.ts @@ -6,7 +6,7 @@ describe("Testing Queue data structure", () => { const queue = new LinkedQueue(); queue.enqueue(1); - expect(queue.length).toBe(1); + expect(queue.length()).toBe(1); }); it("isEmpty should return true on empty queue", () => { @@ -21,7 +21,18 @@ describe("Testing Queue data structure", () => { expect(queue.isEmpty()).toBeFalsy(); }); + it("front should return the first value", () => { + const queue = new LinkedQueue(); + queue.enqueue(1); + + expect(queue.peek()).toBe(1); + }); + + it("front should return null when the queue is empty", () => { + const queue = new LinkedQueue(); + expect(queue.peek()).toBe(undefined); + }); it("length should return the number of elements in the queue", () => { const queue = new LinkedQueue(); @@ -29,7 +40,7 @@ describe("Testing Queue data structure", () => { queue.enqueue(1); queue.enqueue(1); - expect(queue.length).toBe(3); + expect(queue.length()).toBe(3); }); it("dequeue should remove the first element", () => { @@ -37,10 +48,14 @@ describe("Testing Queue data structure", () => { queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); + queue.dequeue(); - - expect(queue.length).toBe(3); + expect(queue.length()).toBe(2); }); + it("dequeue should throw error on empty queue", () => { + const queue = new LinkedQueue(); + expect(() => queue.dequeue()).toThrow("Queue Underflow"); + }); }); From dd122200499fd24527947ddf706beed2c5ac0a3f Mon Sep 17 00:00:00 2001 From: co-bby Date: Fri, 6 Jan 2023 15:24:38 -0500 Subject: [PATCH 09/22] fix: queue test problem --- data_structures/linkedlist_queue.ts | 11 ++-- data_structures/test/Queue.test.ts | 64 +++++++++++++++++++ data_structures/test/array_queue.test.ts | 57 +---------------- data_structures/test/linkedlist_queue.test.ts | 61 +----------------- 4 files changed, 75 insertions(+), 118 deletions(-) create mode 100644 data_structures/test/Queue.test.ts diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts index 2e103c9d..dbfe83d2 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linkedlist_queue.ts @@ -21,9 +21,6 @@ type Node = { next?: Node, } - - - export class LinkedQueue implements Queue { public size: number; @@ -67,13 +64,17 @@ export class LinkedQueue implements Queue { // Returns the value of the head - peek(): T | undefined { + 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 + return this.size === 0 } length(): number { diff --git a/data_structures/test/Queue.test.ts b/data_structures/test/Queue.test.ts new file mode 100644 index 00000000..e90dea3d --- /dev/null +++ b/data_structures/test/Queue.test.ts @@ -0,0 +1,64 @@ + +import { Queue } from '../queue'; +type QueueConstructor = new () => Queue +export function test(Queue: QueueConstructor) { + describe("Testing Queue data structure", () => { + it("enqueue should add a new element to the queue", () => { + const queue = new Queue(); + queue.enqueue(1); + + expect(queue.length()).toBe(1); + }); + + it("isEmpty should return true on empty queue", () => { + const queue = new Queue(); + expect(queue.isEmpty()).toBeTruthy(); + }); + + it("isEmpty should return false on not empty queue", () => { + const queue = new Queue(); + queue.enqueue(1); + + expect(queue.isEmpty()).toBeFalsy(); + }); + + it("front should return the first value", () => { + const queue = new Queue(); + queue.enqueue(1); + + expect(queue.peek()).toBe(1); + }); + + it("front should return null when the queue is empty", () => { + const queue = new Queue(); + + expect(queue.peek()).toBe(null); + }); + + it("length should return the number of elements in the queue", () => { + const queue = new Queue(); + 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(); + 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(); + + expect(() => queue.dequeue()).toThrow("Queue Underflow"); + }); + }); + +} diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index ffa87206..2ea8f9dc 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -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(); - 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(); - expect(queue.isEmpty()).toBeTruthy(); - }); - it("isEmpty should return false on not empty queue", () => { - const queue = new ArrayQueue(); - queue.enqueue(1); - - expect(queue.isEmpty()).toBeFalsy(); - }); - - it("front should return the first value", () => { - const queue = new ArrayQueue(); - queue.enqueue(1); - - expect(queue.peek()).toBe(1); - }); - - it("front should return null when the queue is empty", () => { - const queue = new ArrayQueue(); - - expect(queue.peek()).toBe(null); - }); - - it("length should return the number of elements in the queue", () => { - const queue = new ArrayQueue(); - 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(); - 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(); - - expect(() => queue.dequeue()).toThrow("Queue Underflow"); - }); -}); diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts index b182c250..6dba8d12 100644 --- a/data_structures/test/linkedlist_queue.test.ts +++ b/data_structures/test/linkedlist_queue.test.ts @@ -1,61 +1,6 @@ +import { test } from './Queue.test' -import { LinkedQueue } from './../linkedlist_queue'; +import { LinkedQueue } from '../linkedlist_queue'; -describe("Testing Queue data structure", () => { - it("enqueue should add a new element to the queue", () => { - const queue = new LinkedQueue(); - queue.enqueue(1); +test(LinkedQueue); - expect(queue.length()).toBe(1); - }); - - it("isEmpty should return true on empty queue", () => { - const queue = new LinkedQueue(); - expect(queue.isEmpty()).toBeTruthy(); - }); - - it("isEmpty should return false on not empty queue", () => { - const queue = new LinkedQueue(); - queue.enqueue(1); - - expect(queue.isEmpty()).toBeFalsy(); - }); - - it("front should return the first value", () => { - const queue = new LinkedQueue(); - queue.enqueue(1); - - expect(queue.peek()).toBe(1); - }); - - it("front should return null when the queue is empty", () => { - const queue = new LinkedQueue(); - - expect(queue.peek()).toBe(undefined); - }); - - it("length should return the number of elements in the queue", () => { - const queue = new LinkedQueue(); - queue.enqueue(1); - queue.enqueue(1); - queue.enqueue(1); - - expect(queue.length()).toBe(3); - }); - - it("dequeue should remove the first element", () => { - const queue = new LinkedQueue(); - 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 LinkedQueue(); - - expect(() => queue.dequeue()).toThrow("Queue Underflow"); - }); -}); From e207606abbc72ed3d5bf728d6feb2114eb9ca5f9 Mon Sep 17 00:00:00 2001 From: co-bby Date: Fri, 6 Jan 2023 22:46:15 -0500 Subject: [PATCH 10/22] fix: change filename to match naming convention --- data_structures/test/array_queue.test.ts | 2 +- data_structures/test/linkedlist_queue.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index 2ea8f9dc..2ac5a3c4 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -1,6 +1,6 @@ import { ArrayQueue } from "../array_queue"; -import { test } from './Queue.test' +import { test } from './queue.test' test(ArrayQueue); diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts index 6dba8d12..58a7795f 100644 --- a/data_structures/test/linkedlist_queue.test.ts +++ b/data_structures/test/linkedlist_queue.test.ts @@ -1,4 +1,4 @@ -import { test } from './Queue.test' +import { test } from './queue.test' import { LinkedQueue } from '../linkedlist_queue'; From c65d5fb1b2ffb9eabc8fec62a2639dc5df655a63 Mon Sep 17 00:00:00 2001 From: co-bby Date: Sat, 7 Jan 2023 22:17:24 -0500 Subject: [PATCH 11/22] fix: leading newline and time complexity --- data_structures/linkedlist_queue.ts | 2 +- data_structures/queue.ts | 1 - data_structures/test/Queue.test.ts | 9 --------- data_structures/test/array_queue.test.ts | 1 - data_structures/test/linkedlist_queue.test.ts | 1 - 5 files changed, 1 insertion(+), 13 deletions(-) diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts index dbfe83d2..b326993c 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linkedlist_queue.ts @@ -2,7 +2,7 @@ * 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). + * The time complexity of the operations is O(1). */ diff --git a/data_structures/queue.ts b/data_structures/queue.ts index bac8c07c..81b3c61f 100644 --- a/data_structures/queue.ts +++ b/data_structures/queue.ts @@ -4,5 +4,4 @@ export interface Queue { peek(): T | undefined | null isEmpty(): boolean length(): number - } \ No newline at end of file diff --git a/data_structures/test/Queue.test.ts b/data_structures/test/Queue.test.ts index e90dea3d..7dd94378 100644 --- a/data_structures/test/Queue.test.ts +++ b/data_structures/test/Queue.test.ts @@ -1,4 +1,3 @@ - import { Queue } from '../queue'; type QueueConstructor = new () => Queue export function test(Queue: QueueConstructor) { @@ -6,7 +5,6 @@ export function test(Queue: QueueConstructor) { it("enqueue should add a new element to the queue", () => { const queue = new Queue(); queue.enqueue(1); - expect(queue.length()).toBe(1); }); @@ -18,20 +16,17 @@ export function test(Queue: QueueConstructor) { it("isEmpty should return false on not empty queue", () => { const queue = new Queue(); queue.enqueue(1); - expect(queue.isEmpty()).toBeFalsy(); }); it("front should return the first value", () => { const queue = new Queue(); queue.enqueue(1); - expect(queue.peek()).toBe(1); }); it("front should return null when the queue is empty", () => { const queue = new Queue(); - expect(queue.peek()).toBe(null); }); @@ -40,7 +35,6 @@ export function test(Queue: QueueConstructor) { queue.enqueue(1); queue.enqueue(1); queue.enqueue(1); - expect(queue.length()).toBe(3); }); @@ -50,15 +44,12 @@ export function test(Queue: QueueConstructor) { 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(); - expect(() => queue.dequeue()).toThrow("Queue Underflow"); }); }); - } diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index 2ac5a3c4..a41d6d67 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -1,5 +1,4 @@ import { ArrayQueue } from "../array_queue"; - import { test } from './queue.test' test(ArrayQueue); diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts index 58a7795f..2eb33ee3 100644 --- a/data_structures/test/linkedlist_queue.test.ts +++ b/data_structures/test/linkedlist_queue.test.ts @@ -1,5 +1,4 @@ import { test } from './queue.test' - import { LinkedQueue } from '../linkedlist_queue'; test(LinkedQueue); From 0703824073348b8c46b3c4bd97e159cb59668f55 Mon Sep 17 00:00:00 2001 From: co-bby Date: Sat, 7 Jan 2023 22:24:45 -0500 Subject: [PATCH 12/22] feat: add jsDoc comments --- data_structures/linkedlist_queue.ts | 31 ++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linkedlist_queue.ts index b326993c..bcd2ef21 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linkedlist_queue.ts @@ -32,7 +32,11 @@ export class LinkedQueue implements Queue { this.size = 0; } - // adds elements to the rear/tail of the Queue + /** + * Adds an item to the queue. + * + * @param item The item being added to the queue. + */ enqueue(item: T): void { const node = { value: item } as Node; // Creates a new node this.size++ // Increase the length of the Queue @@ -48,10 +52,14 @@ export class LinkedQueue implements Queue { } - // Remove elements to the front/head of the Queue + /** + * Removes an item from the queue and returns it. + * + * @throws Queue Underflow if the queue is empty. + * @returns The item that was removed from the queue. + */ dequeue(): T | undefined { - // If there is no head return undefined if (!this.head) { throw new Error("Queue Underflow"); } @@ -63,7 +71,11 @@ export class LinkedQueue implements Queue { } - // Returns the value of the head + /** + * Returns the item at the front of the queue. + * + * @returns The item at the front of the queue or null if the queue is empty. + */ peek(): T | undefined | null { if (this.isEmpty()) { @@ -72,11 +84,20 @@ export class LinkedQueue implements Queue { return this.head?.value; } - // Returns true if the Queue is empty + /** + * Checks if the queue is empty. + * + * @returns {boolean} Whether the queue is empty or not. + */ isEmpty(): boolean { return this.size === 0 } + /** + * Returns the number of items in the queue. + * + * @returns {number} The number of items in the queue. + */ length(): number { return this.size; } From feb549fbf5d5832590ef8f6b65247c9d2909239d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:35:33 +0100 Subject: [PATCH 13/22] Rename Queue.test.ts to queue.test.ts --- data_structures/test/{Queue.test.ts => queue.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/test/{Queue.test.ts => queue.test.ts} (100%) diff --git a/data_structures/test/Queue.test.ts b/data_structures/test/queue.test.ts similarity index 100% rename from data_structures/test/Queue.test.ts rename to data_structures/test/queue.test.ts From e9a6a9dc0de64044bb07062cbac314ac24e611d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:39:33 +0100 Subject: [PATCH 14/22] Update queue.test.ts --- data_structures/test/queue.test.ts | 86 +++++++++++++++--------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/data_structures/test/queue.test.ts b/data_structures/test/queue.test.ts index 7dd94378..1dc7eaec 100644 --- a/data_structures/test/queue.test.ts +++ b/data_structures/test/queue.test.ts @@ -1,55 +1,53 @@ import { Queue } from '../queue'; type QueueConstructor = new () => Queue -export function test(Queue: QueueConstructor) { - describe("Testing Queue data structure", () => { - it("enqueue should add a new element to the queue", () => { - const queue = new Queue(); - queue.enqueue(1); - expect(queue.length()).toBe(1); - }); +export function testQueue(Queue: QueueConstructor) { + it("enqueue should add a new element to the queue", () => { + const queue = new Queue(); + queue.enqueue(1); + expect(queue.length()).toBe(1); + }); - it("isEmpty should return true on empty queue", () => { - const queue = new Queue(); - expect(queue.isEmpty()).toBeTruthy(); - }); + it("isEmpty should return true on empty queue", () => { + const queue = new Queue(); + expect(queue.isEmpty()).toBeTruthy(); + }); - it("isEmpty should return false on not empty queue", () => { - const queue = new Queue(); - queue.enqueue(1); - expect(queue.isEmpty()).toBeFalsy(); - }); + it("isEmpty should return false on not empty queue", () => { + const queue = new Queue(); + queue.enqueue(1); + expect(queue.isEmpty()).toBeFalsy(); + }); - it("front should return the first value", () => { - const queue = new Queue(); - queue.enqueue(1); - expect(queue.peek()).toBe(1); - }); + it("front should return the first value", () => { + const queue = new Queue(); + queue.enqueue(1); + expect(queue.peek()).toBe(1); + }); - it("front should return null when the queue is empty", () => { - const queue = new Queue(); - expect(queue.peek()).toBe(null); - }); + it("front should return null when the queue is empty", () => { + const queue = new Queue(); + expect(queue.peek()).toBe(null); + }); - it("length should return the number of elements in the queue", () => { - const queue = new Queue(); - queue.enqueue(1); - queue.enqueue(1); - queue.enqueue(1); - expect(queue.length()).toBe(3); - }); + it("length should return the number of elements in the queue", () => { + const queue = new Queue(); + 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(); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - queue.dequeue(); - expect(queue.length()).toBe(2); - }); + it("dequeue should remove the first element", () => { + const queue = new Queue(); + 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(); - expect(() => queue.dequeue()).toThrow("Queue Underflow"); - }); + it("dequeue should throw error on empty queue", () => { + const queue = new Queue(); + expect(() => queue.dequeue()).toThrow("Queue Underflow"); }); } From 47e9e7e5ff3efca1c25b06bea38c89a354da1f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:40:57 +0100 Subject: [PATCH 15/22] Update array_queue.test.ts --- data_structures/test/array_queue.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index a41d6d67..c1e0d5af 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -1,6 +1,4 @@ import { ArrayQueue } from "../array_queue"; -import { test } from './queue.test' - -test(ArrayQueue); - +import { testQueue } from './queue.test' +describe("Array Queue", () => testQueue(ArrayQueue)); From 7d71faa9b7679a9a9fbf0730b4ce0fb65fc210fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:41:23 +0100 Subject: [PATCH 16/22] Update and rename linkedlist_queue.test.ts to linked_queue.test.ts --- data_structures/test/linked_queue.test.ts | 4 ++++ data_structures/test/linkedlist_queue.test.ts | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) create mode 100644 data_structures/test/linked_queue.test.ts delete mode 100644 data_structures/test/linkedlist_queue.test.ts diff --git a/data_structures/test/linked_queue.test.ts b/data_structures/test/linked_queue.test.ts new file mode 100644 index 00000000..8a8c6d46 --- /dev/null +++ b/data_structures/test/linked_queue.test.ts @@ -0,0 +1,4 @@ +import { test } from './queue.test' +import { LinkedQueue } from '../linked_queue'; + +describe("Linked Queue", () => testQueue(ArrayQueue)); diff --git a/data_structures/test/linkedlist_queue.test.ts b/data_structures/test/linkedlist_queue.test.ts deleted file mode 100644 index 2eb33ee3..00000000 --- a/data_structures/test/linkedlist_queue.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { test } from './queue.test' -import { LinkedQueue } from '../linkedlist_queue'; - -test(LinkedQueue); - From ddf5c95b3282edf6e9bc3def7d8f4b1d2815ea34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:44:35 +0100 Subject: [PATCH 17/22] Update and rename linkedlist_queue.ts to linked_queue.ts --- .../{linkedlist_queue.ts => linked_queue.ts} | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) rename data_structures/{linkedlist_queue.ts => linked_queue.ts} (84%) diff --git a/data_structures/linkedlist_queue.ts b/data_structures/linked_queue.ts similarity index 84% rename from data_structures/linkedlist_queue.ts rename to data_structures/linked_queue.ts index bcd2ef21..c67c8285 100644 --- a/data_structures/linkedlist_queue.ts +++ b/data_structures/linked_queue.ts @@ -1,19 +1,3 @@ -/** - * 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(1). - - */ - -/** USAGE - -*Printers -*CPU task scheduling -*Callback queue in javascript - -*/ - import { Queue } from "./queue"; type Node = { @@ -21,6 +5,12 @@ type Node = { next?: Node, } +/** + * This is a LinkedList-like implementation of a Queue, + * allowing the operations to be implemented in constant time. + * A Queue is a data structure that follows the FIFO (First-In First-Out) principle: + * The first element that was added to the queue will be the first one to be removed. + */ export class LinkedQueue implements Queue { public size: number; From b46be610430c385f43d60804d7d0a1507ffea912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:46:38 +0100 Subject: [PATCH 18/22] Update linked_queue.test.ts --- data_structures/test/linked_queue.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/test/linked_queue.test.ts b/data_structures/test/linked_queue.test.ts index 8a8c6d46..5688ec68 100644 --- a/data_structures/test/linked_queue.test.ts +++ b/data_structures/test/linked_queue.test.ts @@ -1,4 +1,4 @@ -import { test } from './queue.test' +import { testQueue } from './queue.test' import { LinkedQueue } from '../linked_queue'; describe("Linked Queue", () => testQueue(ArrayQueue)); From e0f9ca63931df3ca2ffd196c3e526c07879627e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 14:48:41 +0100 Subject: [PATCH 19/22] Update linked_queue.test.ts --- data_structures/test/linked_queue.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/test/linked_queue.test.ts b/data_structures/test/linked_queue.test.ts index 5688ec68..d90c2d20 100644 --- a/data_structures/test/linked_queue.test.ts +++ b/data_structures/test/linked_queue.test.ts @@ -1,4 +1,4 @@ import { testQueue } from './queue.test' import { LinkedQueue } from '../linked_queue'; -describe("Linked Queue", () => testQueue(ArrayQueue)); +describe("Linked Queue", () => testQueue(LinkedQueue)); From 83b61d2a1650ec95f1656530649ed9df5896dad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 15:00:31 +0100 Subject: [PATCH 20/22] Rename queue.test.ts to queue.ts --- data_structures/test/{queue.test.ts => queue.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/test/{queue.test.ts => queue.ts} (100%) diff --git a/data_structures/test/queue.test.ts b/data_structures/test/queue.ts similarity index 100% rename from data_structures/test/queue.test.ts rename to data_structures/test/queue.ts From 7f2e3feeafce1b881b7cef09b34d75f41de985f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 15:00:45 +0100 Subject: [PATCH 21/22] Update array_queue.test.ts --- data_structures/test/array_queue.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/test/array_queue.test.ts b/data_structures/test/array_queue.test.ts index c1e0d5af..9bc6f48f 100644 --- a/data_structures/test/array_queue.test.ts +++ b/data_structures/test/array_queue.test.ts @@ -1,4 +1,4 @@ import { ArrayQueue } from "../array_queue"; -import { testQueue } from './queue.test' +import { testQueue } from './queue' describe("Array Queue", () => testQueue(ArrayQueue)); From 2a7ce9c1ccae42d0ea388a0a20e1173fba964f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Jan 2023 15:00:58 +0100 Subject: [PATCH 22/22] Update linked_queue.test.ts --- data_structures/test/linked_queue.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/test/linked_queue.test.ts b/data_structures/test/linked_queue.test.ts index d90c2d20..408c1aa7 100644 --- a/data_structures/test/linked_queue.test.ts +++ b/data_structures/test/linked_queue.test.ts @@ -1,4 +1,4 @@ -import { testQueue } from './queue.test' +import { testQueue } from './queue' import { LinkedQueue } from '../linked_queue'; describe("Linked Queue", () => testQueue(LinkedQueue));