Skip to content

Instantly share code, notes, and snippets.

@odykyi
Created June 23, 2019 14:57
Show Gist options
  • Save odykyi/52825a47c549458324168bace680a7bd to your computer and use it in GitHub Desktop.
Save odykyi/52825a47c549458324168bace680a7bd to your computer and use it in GitHub Desktop.
node js javascript queue implementation example data structure
console.time('queue');
const assert = require('assert'); // for tests
class Queue {
constructor() {
this.list = []
}
isEmpty() {
return !this.list.length;
}
size() {
return this.list.length;
}
front() {
return this.list[0];
}
dequeue() {
return this.list.pop();
}
enqueue(element) {
return this.list.push(element);
}
toString() {
return this.list.toString();
}
toArray() {
return [...this.list];
}
}
const queue = new Queue();
console.log('%O', queue);
assert.equal(queue.enqueue(1), 1);
assert.equal(queue.enqueue(2), 2);
assert.deepEqual(queue.toArray(), [1,2]);
assert.deepEqual(queue.toString(), '1,2');
assert.equal(queue.size(), 2);
assert.equal(queue.front(), 1);
assert.equal(queue.dequeue(), 2);
assert.equal(queue.front(), 1);
assert.equal(queue.dequeue(), 1);
assert.equal(queue.isEmpty(), true);
assert.equal(queue.size(), 0);
console.timeEnd('queue');
// node queue.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment