Pages

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

02 July 2012

C program to implement circular Queue [using Array]

Here is the program to implement circular Queue in C using Array Queue is a Data Structure implemented in (FIFO) First In First 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.

Circular Queue program


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

3 comments:

  1. why i cant debug this programme?

    ReplyDelete
  2. Anonymous14 June, 2013

    Situated at the fringe of short distance to city, Sant
    Ritz at Potong Pasir (Singapore) in District
    13. the interlace condo :: theinterlacecondo.sg ::

    ReplyDelete