-
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
c138007
commit c5005e3
Showing
11 changed files
with
585 additions
and
90 deletions.
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,6 +1,6 @@ | ||
# Data Structures and Algorithms | ||
|
||
[](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js) | ||
[](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js) | ||
[](https://github.com/mesirendon/datastructures-and-algorithms-js/actions/workflows/ci.yml) | ||
|
||
This repository written in JavaScript is a compilation of the most-known data structures and algorithms. | ||
|
@@ -13,56 +13,44 @@ JavaScript is also a flexible language that allows performing actions that could | |
|
||
## The Book | ||
|
||
Read all the content of [**Data Structures and Algorithms** here](content.md) | ||
Read all the content of [**Data Structures and Algorithms** here](./content.md) | ||
|
||
## Using this repository | ||
|
||
### Prerequisites | ||
|
||
You will need to have node installed. I recommend using the [nvm installer script](https://github.com/nvm-sh/nvm#install--update-script), and following their [instructions](https://github.com/nvm-sh/nvm#usage). I recommend using an LTS version. | ||
You will need to have node installed. I recommend using the [nvm installer script](https://github.com/nvm-sh/nvm#install--update-script), and following their [instructions](https://github.com/nvm-sh/nvm#usage). I recommend using the `v20.12.2` version. | ||
|
||
### Clone | ||
|
||
```bash | ||
git clone [email protected]:mesirendon/datastructures-and-algorithms-js.git | ||
``` | ||
|
||
### Install dependencies | ||
|
||
```bash | ||
npm ci | ||
``` | ||
|
||
### Run tests | ||
|
||
#### All Tests | ||
|
||
Running all tests will show a coverage report. | ||
|
||
```bash | ||
npm test | ||
npm t | ||
``` | ||
|
||
#### Specific | ||
|
||
```bash | ||
npm run test:case src/singly-linked-lists/__test__/linked-lists.spec.js | ||
``` | ||
|
||
### Start the app | ||
|
||
You can use the provided index.js as a playground by running in a console the following command. | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
When you modify the [index.js](index.js) file and save it, the service will be reloaded. | ||
When you modify the [index.js](./index.js) file and save it, the service will be reloaded. | ||
|
||
## Disclaimer ⚠️ | ||
|
||
This repository is meant to be used as an educational and research tool. Therefore, you should think twice before copying and pasting the code seen here in your production developments. Also, remember this code is licensed under [MIT License](LICENSE), thus limiting any warranty or liability for its use. | ||
This repository is meant to be used as an educational and research tool. Therefore, you should think twice before copying and pasting the code seen here in your production developments. Also, remember this code is licensed under [MIT License](./LICENSE), thus limiting any warranty or liability for its use. | ||
|
||
## Acknowledgement | ||
|
||
[Trekhleb](https://github.com/trekhleb)'s [JavaScript Algorithms](https://github.com/trekhleb/javascript-algorithms) repository heavily inspires this repository. |
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
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,110 @@ | ||
import QueueNode from "./QueueNode.js"; | ||
|
||
export default class Queue { | ||
/** | ||
* Builds a queue | ||
*/ | ||
constructor() { | ||
/** @var QueueNode */ | ||
this.head = null; | ||
|
||
/** @var QueueNode */ | ||
this.tail = null; | ||
|
||
/** @var Number */ | ||
this.size = 0; | ||
} | ||
|
||
/** | ||
* This queue's size | ||
* @returns {Number} This queue's size | ||
*/ | ||
length() { | ||
return this.size; | ||
} | ||
|
||
/** | ||
* Appends a new node to this queue. | ||
* @param {any} value Value to append | ||
* @returns {Queue} This queue | ||
*/ | ||
enqueue(value) { | ||
const node = new QueueNode(value); | ||
this.size++; | ||
|
||
// The new node is the head if this list is empty | ||
if (!this.head) { | ||
this.head = node; | ||
this.tail = node; | ||
|
||
return this; | ||
} | ||
|
||
// Attach the new node to the end of the queue | ||
this.tail.next = node; | ||
this.tail = node; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Deletes this queue head and returns it if it exists. | ||
* Updates references to the new head and tail if applies. | ||
* @returns {(QueueNode|null)} | ||
*/ | ||
dequeue() { | ||
if (!this.head) return null; | ||
|
||
let deletedNode = this.head; | ||
if (this.head === this.tail) this.tail = null; | ||
this.head = this.head.next; | ||
this.size--; | ||
|
||
return deletedNode; | ||
} | ||
|
||
/** | ||
* Returns the value of this queue head | ||
* @returns {any} This queue head's value | ||
*/ | ||
peek() { | ||
if (!this.head) return null; | ||
|
||
return this.head.value; | ||
} | ||
|
||
/** | ||
* Determines if this queue is empty. | ||
* @returns {boolean} | ||
*/ | ||
isEmpty() { | ||
return this.head == null; | ||
} | ||
|
||
/** | ||
* Represents this queue as an array of its nodes. | ||
* @returns {QueueNode[]} Array of nodes in this queue | ||
*/ | ||
toArray() { | ||
const nodes = []; | ||
|
||
let currentNode = this.head; | ||
while (currentNode) { | ||
nodes.push(currentNode); | ||
currentNode = currentNode.next; | ||
} | ||
|
||
return nodes; | ||
} | ||
|
||
/** | ||
* Returns a string representing this queue separated by commas. | ||
* @param {Function} stringifierFn Custom stringifier function | ||
* @returns {string} This queue string representation | ||
*/ | ||
toString(stringifierFn) { | ||
return this.toArray() | ||
.map((n) => n.toString(stringifierFn)) | ||
.toString(); | ||
} | ||
} |
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,11 +1,22 @@ | ||
export default class QueueNode { | ||
/** | ||
* | ||
* Builds a Queue Node | ||
* @param {any} value Queue Node Value | ||
* @param {QueueNode} next Next Queue node | ||
*/ | ||
constructor(value, next = null) { | ||
this.value = value; | ||
this.next = next; | ||
} | ||
|
||
/** | ||
* Returns a string representation of this node. | ||
* | ||
* Could be specified through a function. | ||
* @param {function} customFn Custom stringifier | ||
* @returns {string} This node string representation | ||
*/ | ||
toString(customFn = null) { | ||
return customFn ? customFn(this.value) : `${this.value}`; | ||
} | ||
} |
Oops, something went wrong.