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

[pull] master from amejiarosario:master #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 8 commits into lalittolani:master from amejiarosario:master
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(queue): add front, back and peek methods
  • Loading branch information
amejiarosario committed Aug 28, 2020
commit 04aa9db26659058c09debe985f94845933a2089d
54 changes: 37 additions & 17 deletions src/data-structures/queues/queue.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
const LinkedList = require('../linked-lists/linked-list');

/* Usage Example:
// tag::snippet[]
const queue = new Queue();

queue.enqueue('a');
queue.enqueue('b');
queue.dequeue(); //↪️ a
queue.enqueue('c');
queue.dequeue(); //↪️ b
queue.dequeue(); //↪️ c
// end::snippet[]
// */

// tag::constructor[]
/**
* Data structure where we add and remove elements in a first-in, first-out (FIFO) fashion
*/
class Queue {
constructor() {
this.items = new LinkedList();
constructor(iterable = []) {
this.items = new LinkedList(iterable);
}
// end::constructor[]

// tag::enqueue[]
/**
* Add element to the queue
* Add element to the back of the queue.
* Runtime: O(1)
* @param {any} item
* @returns {queue} instance to allow chaining.
Expand All @@ -25,7 +38,7 @@ class Queue {

// tag::dequeue[]
/**
* Remove element from the queue
* Remove element from the front of the queue.
* Runtime: O(1)
* @returns {any} removed value.
*/
Expand All @@ -47,23 +60,30 @@ class Queue {
isEmpty() {
return !this.items.size;
}

/**
* Return the most recent value or null if empty.
*/
back() {
if (this.isEmpty()) return null;
return this.items.last.value;
}

/**
* Return oldest value from the queue or null if empty.
* (Peek at the next value to be dequeue)
*/
front() {
if (this.isEmpty()) return null;
return this.items.first.value;
}
}

// Aliases
Queue.prototype.peek = Queue.prototype.front;
Queue.prototype.add = Queue.prototype.enqueue;
Queue.prototype.push = Queue.prototype.enqueue;
Queue.prototype.remove = Queue.prototype.dequeue;
Queue.prototype.pop = Queue.prototype.dequeue;

module.exports = Queue;

/* Usage Example:
// tag::snippet[]
const queue = new Queue();

queue.enqueue('a');
queue.enqueue('b');
queue.dequeue(); //↪️ a
queue.enqueue('c');
queue.dequeue(); //↪️ b
queue.dequeue(); //↪️ c
// end::snippet[]
// */
24 changes: 24 additions & 0 deletions src/data-structures/queues/queue.spec.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,28 @@ describe('Queue', () => {
expect(queue.isEmpty()).toBe(false);
});
});

describe('#back', () => {
it('should return null if empty', () => {
expect(queue.back()).toEqual(null);
});

it('should return newest element', () => {
queue.enqueue('oldest');
queue.enqueue('newest');
expect(queue.back()).toEqual('newest');
});
});

describe('#front', () => {
it('should return null if empty', () => {
expect(queue.front()).toEqual(null);
});

it('should return oldest element', () => {
queue.enqueue('oldest');
queue.enqueue('newest');
expect(queue.front()).toEqual('oldest');
});
});
});

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