From 8058eb52a3521dfb4f6e67993791556a6fe87bbd Mon Sep 17 00:00:00 2001 From: Thinh Trinh Duc Date: Thu, 13 Jun 2024 14:39:13 +0700 Subject: [PATCH] :sparkles: (datastructure) added queue (#7) --- packages/datastructure/CHANGELOG.md | 6 +++ packages/datastructure/package.json | 2 +- packages/datastructure/src/index.ts | 1 + packages/datastructure/src/queue/index.ts | 1 + packages/datastructure/src/queue/queue.ts | 65 +++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 packages/datastructure/src/queue/index.ts create mode 100644 packages/datastructure/src/queue/queue.ts diff --git a/packages/datastructure/CHANGELOG.md b/packages/datastructure/CHANGELOG.md index 690b8d5..56c24b4 100644 --- a/packages/datastructure/CHANGELOG.md +++ b/packages/datastructure/CHANGELOG.md @@ -1,5 +1,11 @@ # @ultify/datastructure +## 0.2.0 + +### Minor Changes + +- Added Queue + ## 0.1.0 ### Minor Changes diff --git a/packages/datastructure/package.json b/packages/datastructure/package.json index c82984c..b1ed25d 100644 --- a/packages/datastructure/package.json +++ b/packages/datastructure/package.json @@ -1,6 +1,6 @@ { "name": "@ultify/datastructure", - "version": "0.1.0", + "version": "0.2.0", "main": "./dist/index.cjs", "module": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/packages/datastructure/src/index.ts b/packages/datastructure/src/index.ts index d39a8e6..002c00a 100644 --- a/packages/datastructure/src/index.ts +++ b/packages/datastructure/src/index.ts @@ -1 +1,2 @@ export * from './stack'; +export * from './queue'; diff --git a/packages/datastructure/src/queue/index.ts b/packages/datastructure/src/queue/index.ts new file mode 100644 index 0000000..cadd6a9 --- /dev/null +++ b/packages/datastructure/src/queue/index.ts @@ -0,0 +1 @@ +export * from './queue'; diff --git a/packages/datastructure/src/queue/queue.ts b/packages/datastructure/src/queue/queue.ts new file mode 100644 index 0000000..6276635 --- /dev/null +++ b/packages/datastructure/src/queue/queue.ts @@ -0,0 +1,65 @@ +/** + * @license MIT + * @copyright 2024 Thinh Trinh Duc + * + * @class + */ +export class Queue { + #array: T[] = []; + + /** + * Create a new queue + */ + constructor(...elements: T[]) { + this.#array = [...elements]; + } + + /** + * Creates a queue from the existing array + * @public + * @static + * @param {Array} [elements] + * @return {Stack} + */ + static fromArray(arr: T[]) { + return new Queue(...arr); + } + + /** + * Queue's size + * @readonly + */ + get size() { + return this.#array.length; + } + + /** + * Check if the queue is empty. + */ + isEmpty() { + return this.#array.length === 0; + } + + /** + * Enqueue new elements, and returns the new size of the queue. + */ + enqueue(...value: T[]) { + return this.#array.push(...value); + } + + /** + * + * Removes the frist element from a queue and returns it. + * If the queue is empty, undefined is returned and the queue is not modified. + */ + dequeue() { + return this.#array.shift(); + } + + /** + * Clear queue + */ + clear() { + this.#array = []; + } +}