Skip to content

Instantly share code, notes, and snippets.

@hbarcelos
Created June 1, 2016 02:19
Show Gist options
  • Save hbarcelos/d1079cf0f00b1f039e85e3b46c34a845 to your computer and use it in GitHub Desktop.
Save hbarcelos/d1079cf0f00b1f039e85e3b46c34a845 to your computer and use it in GitHub Desktop.
'use strict';
var RoundLinkedQueue = function RoundLinkedQueue(maxSize) {
this.maxSize = maxSize;
this.size = 0;
this._root = null;
this._last = null;
}
module.exports = RoundLinkedQueue;
RoundLinkedQueue.createNode = function(data, next) {
next = next || null;
return {
data: data,
next: next
};
};
RoundLinkedQueue.prototype.push = function(data) {
var node = RoundLinkedQueue.createNode(data);
if (this.size < this.maxSize) {
if (this._root === null) {
this._last = this._root = node;
} else {
this._last.next = node;
this._last = node;
}
this.size++;
} else {
var data = this._root.data;
this._root = this._root.next;
this._last.next = node;
this._last = node;
return data;
}
};
RoundLinkedQueue.prototype.pop = function() {
if (this.root === null) {
throw new Error('Cannot pop from empty queue');
}
var data = this._root.data;
this._root = this._root.next;
this.size--;
return data;
};
RoundLinkedQueue.prototype.first = function() {
return this._root.data;
};
RoundLinkedQueue.prototype.last = function() {
return this._last.data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment