Program to implement Circular Queue using Array

Circular queue may be viewed as the array that holds the queue elements in circular rather than as a straight line. That means we may imagine that the first element of the array immediately follows the last elements.

 

# include<stdio.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

main()

{

                int choice;

                while(1)

                {

                                printf("1.Insert\n");

                                printf("2.Delete\n");

                                printf("3.Display\n");

                                printf("4.Quit\n");

                                printf("Enter your choice : ");

                                scanf("%d",&choice);

                                switch(choice)

                                {

                                case 1 :

                                                insert();

                                                break;

                                case 2 :

                                                del();

                                                break;

                                case 3:

                                                display();

                                                break;

                                case 4:

                                                exit(1);

                                default:

                                                printf("Wrong choice\n");

                                }/*End of switch*/

                }/*End of while */

}/*End of main()*/

insert()

{

                int added_item;

                if((front == 0 && rear == MAX-1) || (front == rear+1))

                {

                                printf("Queue Overflow \n");

                                return;

                }

                if (front == -1) 

                {

                                front = 0;

                                rear = 0;

                }

                else

                                if(rear == MAX-1)/*rear is at last position of queue */

                                                rear = 0;

                                else

                                                rear = rear+1;

                printf("Pls Insert the element  in queue : ");

                scanf("%d", &added_item);

                cqueue_arr[rear] = added_item ;

}/* insert() end*/

del()

{

                if (front == -1)

                {

                                printf("Queue Underflow\n");

                                return ;

                }

                printf("Element deleted from queue is : %d\n",cqueue_arr[front]);

                if(front == rear) /* queue has only one element */

                {

                                front = -1;

                                rear=-1;

                }

                else

                                if(front == MAX-1)

                                                front = 0;

                                else

                                                front = front+1;

}/*End of del() */

display()

{

                int front_pos = front,rear_pos = rear;

                if(front == -1)

                {

                                printf("Queue is empty\n");

                                return;

                }

                printf("Queue elements :\n");

                if( front_pos <= rear_pos )

                                while(front_pos <= rear_pos)

                                {

                                                printf("%d ",cqueue_arr[front_pos]);

                                                front_pos++;

                                }

                else

                {

                                while(front_pos <= MAX-1)

                                {

                                                printf("%d ",cqueue_arr[front_pos]);

                                                front_pos++;

                                }

                                front_pos = 0;

                                while(front_pos <= rear_pos)

                                {

                                                printf("%d ",cqueue_arr[front_pos]);

                                                front_pos++;

                                }

                }/*End of else */

                printf("\n");

}/*End of display() */

Output.....

Circular Queue using Array

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @