circular queue c program implementation

circular queue c program implementation. Queue is a data structure which uses FIFO (First in first out) principle. Queue can be implemented as simple queue, dequeue and circular queue. Circular queue c is also implemented as same as simple queue, the main difference is that in circular queue last element will again points to first element as shown in figure. You can also see programs for implementing queue, stack and other data structure programs.

circular queue c

circular queue c program implementation

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 

struct Node 
{
        int Data;
        struct Node* next;
}*rear, *front; 

void delQueue() 
{
      struct Node *temp, *var=rear;
      if(var==rear)
      {
            rear = rear->next;
            free(var);
      }
       else
      printf("\nQueue Empty");
}
void push(int value) 
{
      struct Node *temp;
      temp=(struct Node *)malloc(sizeof(struct Node));
      temp->Data=value;
      if (front == NULL)
      {
           front=temp;
           front->next=NULL;
           rear=front;
      }
      else
      {
          front->next=temp;
          front=temp;
          front->next=rear;
      }
}
void display() 
{
      struct Node *var=rear;
      if(var!=NULL)
      {
           printf("\nElements are as:  ");
            while(var!=front)
           {
                 printf("\t%d",var->Data);
                 var=var->next;
           }
           if(var==front)
           {
                printf("\t%d",var->Data);
           }
     printf("\n");
     }
     else
     printf("\nQueue is Empty");
}
int main(int argc, char *argv[]) 
{
     int i=0;
     front=NULL;
     printf(" \n1. Push to Queue");
     printf(" \n2. Pop from Queue");
     printf(" \n3. Display Data of Queue");
     printf(" \n4. Exit\n");
     while(1)
     {
          printf(" \nChoose Option: ");
          scanf("%d",&i);
          switch(i)
          {
          case 1:
          {
          int value;
          printf("\nEnter a valueber to push into Queue: ");
          scanf("%d",&value);
          push(value);
          display();
          break;
          }
          case 2:
          {
          delQueue();
          display();
          break;
          }
          case 3:
          {
          display();
          break;
          }
          case 4:
          {
          exit(0);
     }
     default:
     {
         printf("\nwrong choice for operation");
     }
   }
 }
} 

Output of circular queue c program implementation:

c program to implmenent circular queue
circular queue c program

If you have another way to implement the circular queue c program, let me know. And to discuss more about the same, comment below.


Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>