***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

    Tuesday, August 16, 2022

    Circular Queue

    Linear queues using arrays have certain drawbacks listed as follows:

    1. The linear queue is of a fixed size.

    2. Poor utilization of memory. (Ex: using only 20 but declaring max size 100)

    3. Array implementation of linear queues leads to the Queue_Full state even though the queue is not actually full.

    A more efficient queue representation is obtained by implementing the array as circular. Elements are added to queue until the end of the array is reached and next element is stored in the first slot of the array if it is empty. The empty slots are filled with new incoming elements even if rear=n-1 (size of array is n). The queue is said to be full only when there are n elements in the queue.



    The functions to add and delete elements are:

     

    void Cqueue :: Add(int Element)

    {

    if(!Full())

    Rear = (Rear + 1) % Max;

    Queue[Rear] = Element;

    Size++;

    }

     

    int Cqueue :: Delete()

    {

    if(!Empty())

    Front = (Front + 1) % Max;

    Size--;

    return(Queue[Front]);

    }

     

    Advantages of Using Circular Queues

    1. Data shifting is avoided as the front and rear are modified by using the mod() function. The mod() operation wraps the queue back to its beginning.

    2. If the number of elements to be stored in the queue is fixed, the circular queue is advantageous.

    3. Many practical applications such as printer queue, priority queue, and simulations use the circular queue.

    No comments:

    Post a Comment

    Prim’s algorithm for finding MST (Minimum Spanning Tree)

    Prim's algorithm to find minimum cost spanning tree uses the greedy approach. Prim's algorithm, in contrast with Kruskal's algor...