-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0990e7c
commit 8058eb5
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './stack'; | ||
export * from './queue'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './queue'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} | ||
} |