After we finished the small series about the Stack, we start with the Queue.
O(N)
O(N)
O(1)
O(1)
We will use a Singly Linked List to build our Queue.
A (start) ==> B (end)
A
is the next node in lineA
has a pointer (next
) to the next node (B
)B
is the last node we enqueued (= added) to the QueueA
, the next node in line should be B
We need the following parts to build our Queue:
// a Node has a value (`value`) and a pointer to the next node (`next`)
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// a Queue has a length, a start (`start`), an end (`end`)
class Queue {
constructor() {
this.length = 0;
this.start = null;
this.end = null;
}
}
We set up our Queue. Now we need at least two methods within the Queue:
enqueue
dequeue
We will implement our first method for the Queue.
Don’t miss interesting stuff, subscribe!
Hi! I'm Michael 👋 I'm a Mentor & Senior Web Developer - I help you to reach your (career) goals.