Skip to content

Commit 0d6c786

Browse files
committed
Add queue implementation
1 parent 6da06ad commit 0d6c786

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

queue.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Queue {
2+
constructor() {
3+
this.items = [];
4+
}
5+
6+
enqueue(item) {
7+
this.items.push(item);
8+
}
9+
10+
dequeue() {
11+
return this.items.shift();
12+
}
13+
14+
size() {
15+
return this.items.length;
16+
}
17+
18+
peek() {
19+
return this.items[0];
20+
}
21+
}
22+
23+
const queue = new Queue();
24+
queue.enqueue(5); // Queue state: [5]
25+
queue.enqueue(10); // Queue state: [5, 10]
26+
queue.enqueue(13); // Queue state: [5, 10, 13]
27+
console.log(queue.peek()); // Output: 5
28+
console.log(queue.dequeue()); // Output: 5, Queue State: [10, 13]
29+
console.log(queue.peek()); // Output: 10
30+
console.log(queue.size()); // Output: 2

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Code accompanying my illustrated Data structures [video series on YouTube](https
55
* [What are Data Structures?](https://www.youtube.com/watch?v=9rhT3P1MDHk)[**Watch Video**](https://www.youtube.com/watch?v=9rhT3P1MDHk)
66
* [Array](./array.js)[**Watch Video**](https://www.youtube.com/watch?v=QJNwK2uJyGs)
77
* [Linked List](./linked-list.js)[**Watch Video**](https://www.youtube.com/watch?v=odW9FU8jPRQ)
8-
* [Stack](./stack.js)[Soon on YouTube](https://www.youtube.com/theroadmap?sub_confirmation=1)
9-
* Queue — [Soon on YouTube](https://www.youtube.com/theroadmap?sub_confirmation=1)
8+
* [Stack](./stack.js)[**Watch Video**](https://www.youtube.com/watch?v=I5lq6sCuABE)
9+
* [Queue](./queue.js)[Soon on YouTube](https://www.youtube.com/theroadmap?sub_confirmation=1)
1010
* Heap — [Soon on YouTube](https://www.youtube.com/theroadmap?sub_confirmation=1)
1111
* Hash Table — [Soon on YouTube](https://www.youtube.com/theroadmap?sub_confirmation=1)
1212
* Tree — [Soon on YouTube](https://www.youtube.com/theroadmap?sub_confirmation=1)

0 commit comments

Comments
 (0)