joelbarbosa
11/29/2018 - 11:25 PM

queue

queue


function createQueue() {
  const queue = [];
  
  return {
    enqueue(item) {
      queue.unshift(item);
    },
    dequeue() {
      return queue.pop();
    },
    peek() {
      return queue[queue.length - 1];
    },
    get length() {
      return queue.length;
    },
    isEmpty() {
      return queue.length === 0;
    },
    toString() {
      console.log(queue);
    }
  }
}

const queue = createQueue();
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

console.log('peek: '+ queue.peek())
console.log('lenght: '+ queue.length)
console.log(queue.toString());
console.log('isEmpty: ' + queue.isEmpty())

queue.dequeue();
queue.dequeue();
queue.dequeue();
console.log('peek: '+ queue.peek())
console.log('lenght: '+ queue.length)
console.log(queue.toString());
console.log('isEmpty: ' + queue.isEmpty())