// This file defines a basic queue data structure, providing
// the standard Array push(), shift(), and length attributes,
// but with O(1) operation.
// Private helper class.
var Cell = (function () {
function Cell(item) {
this.item = item;
this.next = null;
}
return Cell;
})();
var Queue = (function () {
function Queue() {
var _this = this;
this.back_ = null;
this.front_ = null;
this.length = 0;
// Add an item to the back of the queue.
this.push = function (item) {
var cell = new Cell(item);
if (_this.length > 0) {
_this.back_.next = cell;
}
else {
// The queue was empty, so set both pointers.
_this.front_ = cell;
}
_this.back_ = cell;
_this.length++;
};
// Remove and return the front element.
this.shift = function () {
var dequeued = _this.front_;
// If this.front_ is this.back_, then getNext() returns null.
_this.front_ = dequeued.next;
dequeued.next = null; // Just to help the garbage collector.
_this.length--;
if (_this.length === 0) {
_this.back_ = null;
}
return dequeued.item;
};
}
return Queue;
})();
exports.Queue = Queue; // class Queue
|