Skip to content

Commit

Permalink
Queues
Browse files Browse the repository at this point in the history
  • Loading branch information
mesirendon committed Jul 28, 2024
1 parent c138007 commit c5005e3
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 90 deletions.
24 changes: 6 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Structures and Algorithms

[![codecov](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js/branch/main/graph/badge.svg?token=G7ACV97AFD)](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js)
[![codecov](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js/graph/badge.svg?token=G7ACV97AFD)](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js)
[![CI](https://github.com/mesirendon/datastructures-and-algorithms-js/actions/workflows/ci.yml/badge.svg?branch=main)](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.
Expand All @@ -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.
35 changes: 19 additions & 16 deletions content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

# Content

- [Algorithmic Complexity](src/algorithmic-complexity/Algorithmic%20Complexity.ipynb)
- [Data Structures](src/data-structures/README.md)

- [Singly Linked List](src/data-structures/singly-linked-lists/Linked%20Lists.ipynb)

- [Algorithmic Complexity](./src/algorithmic-complexity/Algorithmic%20Complexity.ipynb)
- [Data Structures](./src/data-structures/README.md)
- [Singly Linked List](./src/data-structures/singly-linked-lists/Linked%20Lists.ipynb)
- Implementation
- [Singly Linked List Node](src/data-structures/singly-linked-lists/LinkedListNode.js)
- [Singly Linked List Wrapper](src/data-structures/singly-linked-lists/LinkedList.js)
- [Singly Linked List Node](./src/data-structures/singly-linked-lists/LinkedListNode.js)
- [Singly Linked List Wrapper](./src/data-structures/singly-linked-lists/LinkedList.js)
- Tests
- [Singly Linked List Node and Wrapper](src/data-structures/singly-linked-lists/__test__/linked-lists.spec.js)
- [List of Exercises](src/data-structures/singly-linked-lists/exercises/README.md)

- [Doubly Linked List](src/data-structures/doubly-linked-lists/Doubly%20Linked%20Lists.ipynb)
- [Singly Linked List Node and Wrapper](./src/data-structures/singly-linked-lists/__test__/linked-lists.spec.js)
- [List of Exercises](./src/data-structures/singly-linked-lists/exercises/README.md)
- [Doubly Linked List](./src/data-structures/doubly-linked-lists/Doubly%20Linked%20Lists.ipynb)
- Implementation
- [Doubly Linked List Node](./src/data-structures/doubly-linked-lists/DoublyLinkedListNode.js)
- [Doubly Linked List Wrapper](./src/data-structures/doubly-linked-lists/DoublyLinkedList.js)
- Tests
- [Doubly Linked List Node](./src/data-structures/doubly-linked-lists/__test__/doubly-linked-list-node.spec.js)
- [Doubly Linked List Wrapper](./src/data-structures/doubly-linked-lists/__test__/doubly-linked-list.spec.js)
- [Queue](./src/data-structures/queues/Queues.ipynb)
- Implementation
- [Doubly Linked List Node](src/data-structures/doubly-linked-lists/DoublyLinkedListNode.js)
- [Doubly Linked List Wrapper](src/data-structures/doubly-linked-lists/DoublyLinkedList.js)
- [Queue Node](./src/data-structures/queues/QueueNode.js)
- [Queue Wrapper](./src/data-structures/queues/Queue.js)
- Tests
- [Doubly Linked List Node](src/data-structures/doubly-linked-lists/__test__/doubly-linked-list-node.spec.js)
- [Doubly Linked List Wrapper](src/data-structures/doubly-linked-lists/__test__/doubly-linked-list.spec.js)
- **TODO:** List of exercises`]
- [Queue Node and Queue Wrapper](./src/data-structures/queues/__test__/queue.spec.js)
- **TODO:** List of exercises
1 change: 0 additions & 1 deletion src/data-structures/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[Back to Content](../../content.md)

# Data Structures

Data is a broad term that could mean any type of information. Structuring such information is related to organizing and storing it for future access or manipulation in an efficient manner.

**Note on exercises**: I'm not using the actual implementation here because
Expand Down
110 changes: 110 additions & 0 deletions src/data-structures/queues/Queue.js
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();
}
}
13 changes: 12 additions & 1 deletion src/data-structures/queues/QueueNode.js
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}`;
}
}
Loading

0 comments on commit c5005e3

Please sign in to comment.