Home Previous year paper Algorithms Notes About us

Quick Links

Data Structure

Advanced Data Structure

Algorithms

Queue

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.


Operations on Queue

There are two fundamental operations performed on a Queue:

Types of Queue

There are four types of Queues:

  1. Linear Queue:

    In Linear Queue, an insertion takes place from one end while the deletion occurs from another end. The end at which the insertion takes place is known as the rear end, and the end at which the deletion takes place is known as front end. It strictly follows the FIFO rule. The linear Queue can be represented, as shown in the below figure:

    In the above figure, we can observe that the front pointer points to the next element, and the element which was previously pointed by the front pointer was deleted.
    The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first three elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear Queue. In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the Queue.

  2. Circular Queue:

    In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except that the last element of the queue is connected to the first element. It is also known as Ring Buffer as all the ends are connected to another end. The circular queue can be represented as:

    The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty space is available in a circular queue, the new element can be added in an empty space by simply incrementing the value of rear.
    To know more about circular queue:Click Here

  3. Priority Queue:

    A priority queue is another special type of Queue data structure in which each element has some priority associated with it. Based on the priority of the element, the elements are arranged in a priority queue. If the elements occur with the same priority, then they are served according to the FIFO principle. In priority Queue, the insertion takes place based on the arrival while the deletion occurs based on the priority. The priority Queue can be shown as:
    The above figure shows that the highest priority element comes first and the elements of the same priority are arranged based on FIFO structure.

    To know more about priority queue:Click Here
  4. Deque: Both the Linear Queue and Deque are different as the linear queue follows the FIFO principle whereas, deque does not follow the FIFO principle. In Deque, the insertion and deletion can occur from both ends.
    To know more about Deque:Click Here

Array Implementation Of Queue

We can easily represent queue by using linear arrays. There are two variables i.e. front and rear, that are implemented in the case of every queue. Front and rear variables point to the position from where insertions and deletions are performed in a queue. Initially, the value of front and queue is -1 which represents an empty queue. Array representation of a queue containing 5 elements along with the respective values of front and rear, is shown in the following figure.

Insert any element in a queue

ALGORITHM


  • Step 1:

    IF REAR = MAX - 1
    Write OVERFLOW
    Go to step
    [END OF IF]

  • Step 2:

    IF FRONT = -1 and REAR = -1
    SET FRONT = REAR = 0
    ELSE
    SET REAR = REAR + 1
    [END OF IF]

  • Step 3:

    Set QUEUE[REAR] = NUM

  • Step 4:

    EXIT

Delete an element from the queue

ALGORITHM


  • Step 1:

    IF FRONT = -1 or FRONT > REAR
    Write UNDERFLOW
    ELSE
    SET VAL = QUEUE[FRONT]
    SET FRONT = FRONT + 1
    [END OF IF]

  • Step 2:

    EXIT


Drawbacks of Array Implementatin

Although, the technique of creating a queue is easy, but there are some drawbacks of using this technique to implement a queue.

Linked List Implementation Of Queue

The storage requirement of linked representation of a queue with n elements is o(n) while the time requirement for operations is o(1).
In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each element of the queue points to its immediate next element in the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer. The front pointer contains the address of the starting element of the queue while the rear pointer contains the address of the last element of the queue.


Insert Operation

ALGORITHM


  • Step 1:

    Allocate the space for the new node PTR

  • Step 2:

    SET PTR -> DATA = VAL

  • Step 3:

    IF FRONT = NULL
    SET FRONT = REAR = PTR
    SET FRONT -> NEXT = REAR -> NEXT = NULL
    ELSE
    SET REAR -> NEXT = PTR
    SET REAR = PTR
    SET REAR -> NEXT = NULL
    [END OF IF]

  • Step 4:

    END


Deletion

Deletion operation removes the element that is first inserted among all the queue elements. Firstly, we need to check either the list is empty or not. The condition front == NULL becomes true if the list is empty, in this case , we simply write underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy the node pointed by the front pointer into the pointer ptr. Now, shift the front pointer, point to its next node and free the node pointed by the node ptr. This is done by using the following statements.

ALGORITHM


  • Step 1:

    IF FRONT = NULL
    Write " Underflow "
    Go to Step 5
    [END OF IF]

  • Step 2:

    SET PTR = FRONT

  • Step 3:

    SET FRONT = FRONT -> NEXT

  • Step 4:

    FREE PTR

  • Step 5:

    END


Practice Problems On Queues