// empty queueconstqueue=newQueue();// from an arrayconstqueue=newQueue([1,2,3]);
using "Queue.fromArray(array)"
Example
// empty queueconstqueue=Queue.fromArray([]);// with elementsconstlist=[10,3,8,40,1];constqueue=Queue.fromArray(list);// If the list should not be mutated, simply construct the queue from a copy of it.constqueue=Queue.fromArray(list.slice(0));
.enqueue(element)
adds an element at the back of the queue.
params
name
type
element
object
runtime
O(1)
Example
queue.enqueue(10);queue.enqueue(20);
.front()
peeks on the front element of the queue.
return
object
runtime
O(1)
Example
console.log(queue.front());// 10
.back()
peeks on the back element in the queue.
return
object
runtime
O(1)
Example
console.log(queue.back());// 20
.dequeue()
dequeue the front element in the queue. It does not use .shift() to dequeue an element. Instead, it uses a pointer to get the front element and only remove elements when reaching half size of the queue.