Skip to content

Commit c5005e3

Browse files
committed
Queues
1 parent c138007 commit c5005e3

File tree

11 files changed

+585
-90
lines changed

11 files changed

+585
-90
lines changed

README.md

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Data Structures and Algorithms
22

3-
[![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)
3+
[![codecov](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js/graph/badge.svg?token=G7ACV97AFD)](https://codecov.io/gh/mesirendon/datastructures-and-algorithms-js)
44
[![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)
55

66
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
1313

1414
## The Book
1515

16-
Read all the content of [**Data Structures and Algorithms** here](content.md)
16+
Read all the content of [**Data Structures and Algorithms** here](./content.md)
1717

1818
## Using this repository
19-
2019
### Prerequisites
21-
22-
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.
20+
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.
2321

2422
### Clone
25-
2623
```bash
2724
git clone [email protected]:mesirendon/datastructures-and-algorithms-js.git
2825
```
2926

3027
### Install dependencies
31-
3228
```bash
3329
npm ci
3430
```
3531

3632
### Run tests
37-
3833
#### All Tests
39-
4034
Running all tests will show a coverage report.
41-
4235
```bash
43-
npm test
36+
npm t
4437
```
4538

4639
#### Specific
47-
4840
```bash
4941
npm run test:case src/singly-linked-lists/__test__/linked-lists.spec.js
5042
```
5143

5244
### Start the app
53-
5445
You can use the provided index.js as a playground by running in a console the following command.
55-
5646
```bash
5747
npm start
5848
```
5949

60-
When you modify the [index.js](index.js) file and save it, the service will be reloaded.
50+
When you modify the [index.js](./index.js) file and save it, the service will be reloaded.
6151

6252
## Disclaimer ⚠️
63-
64-
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.
53+
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.
6554

6655
## Acknowledgement
67-
6856
[Trekhleb](https://github.com/trekhleb)'s [JavaScript Algorithms](https://github.com/trekhleb/javascript-algorithms) repository heavily inspires this repository.

content.md

+19-16
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
# Content
44

5-
- [Algorithmic Complexity](src/algorithmic-complexity/Algorithmic%20Complexity.ipynb)
6-
- [Data Structures](src/data-structures/README.md)
7-
8-
- [Singly Linked List](src/data-structures/singly-linked-lists/Linked%20Lists.ipynb)
9-
5+
- [Algorithmic Complexity](./src/algorithmic-complexity/Algorithmic%20Complexity.ipynb)
6+
- [Data Structures](./src/data-structures/README.md)
7+
- [Singly Linked List](./src/data-structures/singly-linked-lists/Linked%20Lists.ipynb)
108
- Implementation
11-
- [Singly Linked List Node](src/data-structures/singly-linked-lists/LinkedListNode.js)
12-
- [Singly Linked List Wrapper](src/data-structures/singly-linked-lists/LinkedList.js)
9+
- [Singly Linked List Node](./src/data-structures/singly-linked-lists/LinkedListNode.js)
10+
- [Singly Linked List Wrapper](./src/data-structures/singly-linked-lists/LinkedList.js)
1311
- Tests
14-
- [Singly Linked List Node and Wrapper](src/data-structures/singly-linked-lists/__test__/linked-lists.spec.js)
15-
- [List of Exercises](src/data-structures/singly-linked-lists/exercises/README.md)
16-
17-
- [Doubly Linked List](src/data-structures/doubly-linked-lists/Doubly%20Linked%20Lists.ipynb)
12+
- [Singly Linked List Node and Wrapper](./src/data-structures/singly-linked-lists/__test__/linked-lists.spec.js)
13+
- [List of Exercises](./src/data-structures/singly-linked-lists/exercises/README.md)
14+
- [Doubly Linked List](./src/data-structures/doubly-linked-lists/Doubly%20Linked%20Lists.ipynb)
15+
- Implementation
16+
- [Doubly Linked List Node](./src/data-structures/doubly-linked-lists/DoublyLinkedListNode.js)
17+
- [Doubly Linked List Wrapper](./src/data-structures/doubly-linked-lists/DoublyLinkedList.js)
18+
- Tests
19+
- [Doubly Linked List Node](./src/data-structures/doubly-linked-lists/__test__/doubly-linked-list-node.spec.js)
20+
- [Doubly Linked List Wrapper](./src/data-structures/doubly-linked-lists/__test__/doubly-linked-list.spec.js)
21+
- [Queue](./src/data-structures/queues/Queues.ipynb)
1822
- Implementation
19-
- [Doubly Linked List Node](src/data-structures/doubly-linked-lists/DoublyLinkedListNode.js)
20-
- [Doubly Linked List Wrapper](src/data-structures/doubly-linked-lists/DoublyLinkedList.js)
23+
- [Queue Node](./src/data-structures/queues/QueueNode.js)
24+
- [Queue Wrapper](./src/data-structures/queues/Queue.js)
2125
- Tests
22-
- [Doubly Linked List Node](src/data-structures/doubly-linked-lists/__test__/doubly-linked-list-node.spec.js)
23-
- [Doubly Linked List Wrapper](src/data-structures/doubly-linked-lists/__test__/doubly-linked-list.spec.js)
24-
- **TODO:** List of exercises`]
26+
- [Queue Node and Queue Wrapper](./src/data-structures/queues/__test__/queue.spec.js)
27+
- **TODO:** List of exercises

src/data-structures/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[Back to Content](../../content.md)
22

33
# Data Structures
4-
54
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.
65

76
**Note on exercises**: I'm not using the actual implementation here because

src/data-structures/queues/Queue.js

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import QueueNode from "./QueueNode.js";
2+
3+
export default class Queue {
4+
/**
5+
* Builds a queue
6+
*/
7+
constructor() {
8+
/** @var QueueNode */
9+
this.head = null;
10+
11+
/** @var QueueNode */
12+
this.tail = null;
13+
14+
/** @var Number */
15+
this.size = 0;
16+
}
17+
18+
/**
19+
* This queue's size
20+
* @returns {Number} This queue's size
21+
*/
22+
length() {
23+
return this.size;
24+
}
25+
26+
/**
27+
* Appends a new node to this queue.
28+
* @param {any} value Value to append
29+
* @returns {Queue} This queue
30+
*/
31+
enqueue(value) {
32+
const node = new QueueNode(value);
33+
this.size++;
34+
35+
// The new node is the head if this list is empty
36+
if (!this.head) {
37+
this.head = node;
38+
this.tail = node;
39+
40+
return this;
41+
}
42+
43+
// Attach the new node to the end of the queue
44+
this.tail.next = node;
45+
this.tail = node;
46+
47+
return this;
48+
}
49+
50+
/**
51+
* Deletes this queue head and returns it if it exists.
52+
* Updates references to the new head and tail if applies.
53+
* @returns {(QueueNode|null)}
54+
*/
55+
dequeue() {
56+
if (!this.head) return null;
57+
58+
let deletedNode = this.head;
59+
if (this.head === this.tail) this.tail = null;
60+
this.head = this.head.next;
61+
this.size--;
62+
63+
return deletedNode;
64+
}
65+
66+
/**
67+
* Returns the value of this queue head
68+
* @returns {any} This queue head's value
69+
*/
70+
peek() {
71+
if (!this.head) return null;
72+
73+
return this.head.value;
74+
}
75+
76+
/**
77+
* Determines if this queue is empty.
78+
* @returns {boolean}
79+
*/
80+
isEmpty() {
81+
return this.head == null;
82+
}
83+
84+
/**
85+
* Represents this queue as an array of its nodes.
86+
* @returns {QueueNode[]} Array of nodes in this queue
87+
*/
88+
toArray() {
89+
const nodes = [];
90+
91+
let currentNode = this.head;
92+
while (currentNode) {
93+
nodes.push(currentNode);
94+
currentNode = currentNode.next;
95+
}
96+
97+
return nodes;
98+
}
99+
100+
/**
101+
* Returns a string representing this queue separated by commas.
102+
* @param {Function} stringifierFn Custom stringifier function
103+
* @returns {string} This queue string representation
104+
*/
105+
toString(stringifierFn) {
106+
return this.toArray()
107+
.map((n) => n.toString(stringifierFn))
108+
.toString();
109+
}
110+
}
+12-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
export default class QueueNode {
22
/**
3-
*
3+
* Builds a Queue Node
44
* @param {any} value Queue Node Value
55
* @param {QueueNode} next Next Queue node
66
*/
77
constructor(value, next = null) {
88
this.value = value;
99
this.next = next;
1010
}
11+
12+
/**
13+
* Returns a string representation of this node.
14+
*
15+
* Could be specified through a function.
16+
* @param {function} customFn Custom stringifier
17+
* @returns {string} This node string representation
18+
*/
19+
toString(customFn = null) {
20+
return customFn ? customFn(this.value) : `${this.value}`;
21+
}
1122
}

0 commit comments

Comments
 (0)