<aside> đź’ˇ Queue Logic

</aside>


// Pseudo code
Queue[] queue = new Queue(10);

// Pretend there are 10 different elements
queue = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

System.out.println(queue.removeFirst());

output = 1;

As you can see the first element (1) - index 0. is the one who’s getting removed first.


Is it always the index 0?

No, it’s the head that is moving.

The element is still there, but it’s spot in marked as “free to use”, because the only thing we do is to increment the head.


When we add elements, is it always at the last index?

No, to keep our operation always O(n) the element is placed on the tail. Then we increment it using it mod the length of the array that the queue is based of. This way we guarantee a constant time inserting an element and never it never goes out of bounds.

There are multiples ways to do it, its up to you if you want to: