C++ programs to implement the Queue ADT using an array

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

/* Write C++ programs to implement the Queue ADT using an array */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
 
class queue
{
              int queue1[5];
              int rear,front;
      public:
              queue()
                {
                     rear=-1;
                     front=-1;
                }
              void insert(int x)
               {
                   if(rear >  4)
                    {
                       cout <<"queue over flow";
                       front=rear=-1;
                       return;
                    }
                    queue1[++rear]=x;
                    cout <<"inserted" <<x;
               }
              void delet()
               {
                   if(front==rear)
                     {
                         cout <<"queue under flow";
                         return;
                     }
                     cout <<"deleted" <<queue1[++front];
                }
              void display()
               {
                   if(rear==front)
                     {
                          cout <<" queue empty";
                          return;
                     }
                   for(int i=front+1;i<=rear;i++)
                   cout <<queue1[i]<<" ";
               }
};
 
main()
{
      int ch;
      queue qu;
      while(1)
        {
              cout <<"\n1.insert  2.delet  3.display  4.exit\nEnter ur choice";
              cin >> ch;
              switch(ch)
                {
                  case 1:    cout <<"enter the element";
                           	 cin >> ch;
                             qu.insert(ch);
                             break;
                  case 2:  qu.delet();  break;
                  case 3:  qu.display();break;
                  case 4: exit(0);
                  }
          }
return (0);
}

OUTPUT
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21

1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22

1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16

1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16

1.insert 2.delet 3.display 4.exit
Enter ur choice2
deleted21

1.insert 2.delet 3.display 4.exit
Enter ur choice3
22 16

1.insert 2.delet 3.display 4.exit
Enter ur choice

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

18 Responses to “C++ programs to implement the Queue ADT using an array”

  1. why is it for(int i=front+1;i<=rear;i++) and not for(int i=front;i<=rear;i++) before the main function?

    Reply
  2. sulthan the warrior

    yah….i am very glad about your solution for programs……

    Reply
  3. thanks alot.. please email me the limplemmentation of the linked list i’ll be very thankful to u …

    Reply
  4. ikramlim

    thx for help. how about if i want to enter character? i already change it data type but also error.. please for help..

    Reply
  5. what is the use of while in this example or why use while(1) except while(2) or while(3) please answer !!

    Reply

Leave a Reply