Skip to content

Commit

Permalink
✨ (datastructure) added queue (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhthinh388 authored Jun 13, 2024
1 parent 0990e7c commit 8058eb5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/datastructure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ultify/datastructure

## 0.2.0

### Minor Changes

- Added Queue

## 0.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/datastructure/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/datastructure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './stack';
export * from './queue';
1 change: 1 addition & 0 deletions packages/datastructure/src/queue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './queue';
65 changes: 65 additions & 0 deletions packages/datastructure/src/queue/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license MIT
* @copyright 2024 Thinh Trinh Duc <[email protected]>
*
* @class
*/
export class Queue<T> {
#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<T>(arr: T[]) {
return new Queue<T>(...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 = [];
}
}

0 comments on commit 8058eb5

Please sign in to comment.