Last active
December 4, 2018 22:00
-
-
Save joelbarbosa/702ece6df24f8ea796d62fd7a7f96fa1 to your computer and use it in GitHub Desktop.
queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment