Understanding queue

The queue is another special linear data structure that follows the First-In, First-Out (FIFO) principle. There are two ends for the operation: one to append to the queue and one to remove from the queue. This is different from a stack, where we used one end for both the add and remove operations. The insertion will always be at the back or rear section. The removal of an element will take place from the frontend. The process of adding a new element to the queue is known as enqueue and the process of removing an element is known as dequeue. The process of looking at the front element of the queue without removing the element is known as a peek, similar to the top operation of a stack. The following figure depicts a representation of a queue:

Now, if we define an interface for a queue, it will look like this:

interface Queue { 

public function enqueue(string $item);

public function dequeue();

public function peek();

public function isEmpty();
}

Now we can implement the queue using different methods, as we did for the stack. First, we are going to implement the queue using a PHP array, followed by LinkedList, and then SplQueue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset