Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04aa9db

Browse files
feat(queue): add front, back and peek methods
1 parent 263c9dc commit 04aa9db

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

‎src/data-structures/queues/queue.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
const LinkedList = require('../linked-lists/linked-list');
22

3+
/* Usage Example:
4+
// tag::snippet[]
5+
const queue = new Queue();
6+
7+
queue.enqueue('a');
8+
queue.enqueue('b');
9+
queue.dequeue(); //↪️ a
10+
queue.enqueue('c');
11+
queue.dequeue(); //↪️ b
12+
queue.dequeue(); //↪️ c
13+
// end::snippet[]
14+
// */
15+
316
// tag::constructor[]
417
/**
518
* Data structure where we add and remove elements in a first-in, first-out (FIFO) fashion
619
*/
720
class Queue {
8-
constructor() {
9-
this.items = new LinkedList();
21+
constructor(iterable=[]) {
22+
this.items = new LinkedList(iterable);
1023
}
1124
// end::constructor[]
1225

1326
// tag::enqueue[]
1427
/**
15-
* Add element to the queue
28+
* Add element to the back of the queue.
1629
* Runtime: O(1)
1730
* @param {any} item
1831
* @returns {queue} instance to allow chaining.
@@ -25,7 +38,7 @@ class Queue {
2538

2639
// tag::dequeue[]
2740
/**
28-
* Remove element from the queue
41+
* Remove element from the front of the queue.
2942
* Runtime: O(1)
3043
* @returns {any} removed value.
3144
*/
@@ -47,23 +60,30 @@ class Queue {
4760
isEmpty() {
4861
return !this.items.size;
4962
}
63+
64+
/**
65+
* Return the most recent value or null if empty.
66+
*/
67+
back() {
68+
if (this.isEmpty()) return null;
69+
return this.items.last.value;
70+
}
71+
72+
/**
73+
* Return oldest value from the queue or null if empty.
74+
* (Peek at the next value to be dequeue)
75+
*/
76+
front() {
77+
if (this.isEmpty()) return null;
78+
return this.items.first.value;
79+
}
5080
}
5181

5282
// Aliases
83+
Queue.prototype.peek = Queue.prototype.front;
5384
Queue.prototype.add = Queue.prototype.enqueue;
85+
Queue.prototype.push = Queue.prototype.enqueue;
5486
Queue.prototype.remove = Queue.prototype.dequeue;
87+
Queue.prototype.pop = Queue.prototype.dequeue;
5588

5689
module.exports = Queue;
57-
58-
/* Usage Example:
59-
// tag::snippet[]
60-
const queue = new Queue();
61-
62-
queue.enqueue('a');
63-
queue.enqueue('b');
64-
queue.dequeue(); //↪️ a
65-
queue.enqueue('c');
66-
queue.dequeue(); //↪️ b
67-
queue.dequeue(); //↪️ c
68-
// end::snippet[]
69-
// */

‎src/data-structures/queues/queue.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,28 @@ describe('Queue', () => {
4646
expect(queue.isEmpty()).toBe(false);
4747
});
4848
});
49+
50+
describe('#back', () => {
51+
it('should return null if empty', () => {
52+
expect(queue.back()).toEqual(null);
53+
});
54+
55+
it('should return newest element', () => {
56+
queue.enqueue('oldest');
57+
queue.enqueue('newest');
58+
expect(queue.back()).toEqual('newest');
59+
});
60+
});
61+
62+
describe('#front', () => {
63+
it('should return null if empty', () => {
64+
expect(queue.front()).toEqual(null);
65+
});
66+
67+
it('should return oldest element', () => {
68+
queue.enqueue('oldest');
69+
queue.enqueue('newest');
70+
expect(queue.front()).toEqual('oldest');
71+
});
72+
});
4973
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /