Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to illustrate the implementation of array as a Circular Queue

Posted By: Easy Tutor     Category: C++ Programming     Views: 4074

A C++ Program to illustrate the implementation of array as a Circular Queue.

Code for Program to illustrate the implementation of array as a Circular Queue in C++ Programming

 #include <iostream.h>
 #include    <conio.h>

 const max_length=10;

 class Queue
    {
       private:
      int queue[max_length];
      int rear;
      int front;

       public:
      Queue( );

      void Delete( );
      void Insert( );
      void print_queue( );
      void show_working( );
    };

 /*************************************************************************///--------------------------  Queue( )  ---------------------------------///*************************************************************************/void Queue::Queue( )
    {
       rear=-1;
       front=-1;

       for(int count=0;count<max_length;count++)
      queue[count]=0;
    }

 /*************************************************************************///------------------------------  Insert( )  ----------------------------///*************************************************************************/void Queue::Insert( )
    {
       int item;

       cout<<"\n\n\n\n\n\t Enter value to insert into the Queue : ";
       cin>>item;

       if((rear==front-1) || (front==0 && rear==max_length-1))
      cout<<"\n\n\t ***  Error : Queue is full. \n"<<endl;

       elseif(rear==-1 && front==-1)
      {
         rear++;
         queue[rear]=item;
         front=rear;

         cout<<"\n\n\t *** "<<item<<" is inserted into the Queue."<<endl;
      }

       elseif(rear==max_length-1 && front!=0)
      {
         rear=0;
         queue[rear]=item;

         cout<<"\n\n\t *** "<<item<<" is inserted into the Queue."<<endl;
      }

       else
      {
         rear++;
         queue[rear]=item;

         cout<<"\n\n\t *** "<<item<<" is inserted into the Queue."<<endl;
      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /*************************************************************************///------------------------------  Delete( )  ----------------------------///*************************************************************************/void Queue::Delete( )
    {
       if(rear==-1 && front==-1)
      cout<<"\n\n\n\t ***  Error : Queue is empty. \n"<<endl;

       else
      {
         cout<<"\n\n\n\t *** "<<queue[front]<<" is deleted from the Queue."<<endl;

         queue[front]=0;

         if(front==rear)
        {
           front=-1;
           rear=-1;
        }

         elseif(front==max_length-1)
        front=0;

         else
        front++;
      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /*************************************************************************///---------------------------  print_queue( )  --------------------------///*************************************************************************/void Queue::print_queue( )
    {
       if(front!=-1 && rear!=-1)
      {
         cout<<"\n\n\n\n\n\t Values inserted into the Queue are : \n"<<endl;

         for(int count=0;count<max_length;count++)
        cout<<"\t Stack ["<<count<<"]  =  "<<queue[count]<<endl;
      }

       else
      cout<<"\n\n\n\n\n\t *** Nothing to show. "<<endl;

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }


 /*************************************************************************///-------------------------  show_working( )  ---------------------------///*************************************************************************/void Queue::show_working( )
    {
       char Key=NULL;

       do
      {
         clrscr( );

         gotoxy(5,5);
         cout<<"**********   Implementation of Array as Circular Queue   **********"<<endl;

         gotoxy(10,8);
         cout<<"Select one of the listed operation :"<<endl;

         gotoxy(15,10);
         cout<<"- Press \'I\' to Insert a value"<<endl;

         gotoxy(15,12);
         cout<<"- Press \'D\' to Delete a value"<<endl;

         gotoxy(15,14);
         cout<<"- Press \'P\' to Print the values"<<endl;

         gotoxy(15,16);
         cout<<"- Press \'E\' to Exit"<<endl;

         Input:

         gotoxy(10,20);
         cout<<"                      ";

         gotoxy(10,20);
         cout<<"Enter your Choice : ";

         Key=getche( );

         if(int(Key)==27 || Key=='e' || Key=='E')
        break;

         elseif(Key=='i' || Key=='I')
        Insert( );

         elseif(Key=='d' || Key=='D')
        Delete( );

         elseif(Key=='p' || Key=='P')
        print_queue( );

         elsegoto Input;
      }
       while(1);
    }

 int main( )
    {
       Queue obj;

       obj.show_working( );

       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to illustrate the implementation of array as a Circular Queue is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!