Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

03 July 2012

C program to implement circular Queue [using Structure]

Here is the program to implement circular Queue in C using Structure Queue is a Data Structure implemented in (FIFO) First IFirst Out manner.Unlike in Stack ,Queue requires two variables i.e front and rear which points or indicates where to place new element to be added and from where to delete.Insertion and deletion are performed by operation's Enqueue and Dequeue i.e addition of element to the end and removing the element from front.


C program to implement circular Queue using Structure


Program to implement circular Queue
#include<stdio.h>

#define max 5
struct queue
{
      int front,rear,q[max];
};
void enqueue(struct queue*);
void dequeue(struct queue*);
void qdisplay(struct queue*);
void main(void)
{
       int c;
       struct queue q;
       q.front=q.rear=-1;
       do
       {
             printf("1:insert\n2:deletion\n3:display\n4:exit\nenter choice:");
             scanf("%d",&c);
             switch(c)
                   {
                         case 1:enqueue(&q);break;
                         case 2:dequeue(&q);break;
                         case 3:qdisplay(&q);break;
                         case 4:printf("pgm ends\n");break;
                         default:printf("wrong choice\n");break;
                   }
       }while(c!=4);
       getch();
}
void enqueue(struct queue *p1)
{
       int x;
       if((p1->front==0&&p1->rear==max-1)||(p1->front==p1->rear+1))
C program to implement circular Queue using Structure       {
             printf("q is overflow\n");return;
       }
       if(p1->front==-1)
       {
             p1->front=p1->rear=0;
       }
       else
       {
             if(p1->rear==max-1)
             {
                   p1->rear=0;
             }
             else
             {
                   p1->rear++;
             }
       }
       printf("enter the no:\n");
       scanf("%d",&x);
       p1->q[p1->rear]=x;
       printf("%d succ. inserted\n",x);
       return;
}
void dequeue(struct queue *p2)
{
       int y;
       if(p2->front==-1)
       {
             printf("q is underflow\n");return;
       }
       y=p2->q[p2->front];
       if(p2->front==p2->rear)
       {
             p2->front=p2->rear=-1;
       }
       else
       {
             if(p2->front==max-1)
             {
                   p2->front=0;
             }
             else
             {
                   p2->front++;
             }
       }
       printf("%d succ. deleted\n",y);
       return;
}
void qdisplay(struct queue *p3)
{
       int i,j;
       if(p3->front==p3->rear==-1)
       {
             printf("q is empty\n");return;
       }
       printf("elements are :\n");
       for(i=p3->front;i!=p3->rear;i=(i+1)%max)
       {
             printf("%d\n",p3->q[i]);
       }
       printf("%d\n",p3->q[p3->rear]);
       return; 
}

Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You.
-Romil shah

No comments:

Post a Comment

Back To Top