Question:

Write a program in C language to implement the insertion and deletion operations in a circular queue?

by  |  earlier

0 LIKES UnLike

 Tags:

   Report

1 ANSWERS


  1. /* Circular Queues */
      #include<iostream.h>
      #include<conio.h>

      const int MAX = 5;
      class cqueue
      {
      int a[MAX],front,rear;
      public :
       cqueue()
         {
           front=rear=-1;
         }
         void insert(int );
         int deletion();
         void display();
      };

      void cqueue :: insert(int val)
      {
    if((front==0 && rear==MAX-1) || (rear+1==front))
      cout<<" Circular Queue is Full";

    else
    {
       if(rear==MAX-1)
      rear=0;
       else
    rear++;
       a[rear]=val;
    }
    if(front==-1)
       front=0;
      }
      int cqueue :: deletion()
      {
    int k;
    if(front==-1)
    cout<<"Circular Queue is Empty";
    else
    {
    k=a[front];
    if(front==rear)
       front=rear=-1;
    else
    {
       if(front==MAX-1)
      front=0;
       else
      front++;
    }
    }
    return k;
      }
      void cqueue :: display()
      {
      int i;
      if(front==-1)
    cout<<"Circular Queue is Empty";
      else
      {
    if(rear < front)
    {
    for(i=front;i<=MAX-1;i++)
       cout<<a[i]<<"   ";
    for(i=0;i<=rear;i++)
       cout<<a[i]<<"   ";
    }
    else
    {
    for(i=front;i<=rear;i++)
       cout<<a[i]<<"   ";
    cout<<endl;
    }
      }
      }

      void main()
      {
    cqueue c1;

    int ch,val;
    char op;
    do
    {
       clrscr();
       cout<<"-----------Menu-------------";cout<<endl;
       cout<<".....1.Insertion";cout<<endl;
       cout<<".... 2.Deletion:";cout<<endl;
       cout<<".....3.Display";cout<<endl;
       cout<<".... 4.Exit";cout<<endl;
       cout<<".....Enter Your Choice <1..4> ?"<<endl;
       cin>>ch;
       switch(ch)
       {
      case 1 : cout<<"Enter Element to Insert ?";
    cin>>val;
    c1.insert(val);
    break;
      case 2 : val=c1.deletion();
    cout<<"Deleted Element :"<<val<<endl;
    break;
      case 3 : c1.display();
    break;
    }
    cout<<"Do you want to continue<Y/N> ?"<<endl;
    cin>>op;
      }while(op=='Y' || op=='y');
       getch();
    }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions