Javascript Data Structure Queue via backend array

Description

Javascript Data Structure Queue via backend array

function Queue(array) {
    this.array = [];//from  w  w  w . j  av  a2 s .  com
    if (array) this.array = array;
}

Queue.prototype.getBuffer = function() {
    return this.array.slice();
}

Queue.prototype.isEmpty = function() {
    return this.array.length == 0;
}


Queue.prototype.peek = function() {
    return this.array[0];
}


Queue.prototype.enqueue = function(value) {
    return this.array.push(value);
}


Queue.prototype.dequeue = function() {
    return this.array.shift();
};

//instance of the queue class
var queue1 = new Queue();

console.log(queue1); // { array: [] }
var queue1 = new Queue();

queue1.enqueue(1);
queue1.enqueue(2);
queue1.enqueue(3);

console.log(queue1); // {array: [1,2,3]}

queue1.dequeue();
console.log(queue1); // {array: [2,3]}

queue1.dequeue();
console.log(queue1); // {array: [3]}



PreviousNext

Related