Write a C++ program using STL to implement a queue using SLL.

 Write C++ program using STL for implementation of queue using SLL.


#include<iostream>

#include<queue>


using namespace std;

OUTPUT FOR THE PROGRAM

int main()

{

   int data,i,n;

   int ch; 

   char ans;

   

   queue<int>q;

   do

   {

      cout<<"\nQueue Using STL";

      cout<<"\n1.Insert";

      cout<<"\n2.Delete";

      cout<<"\n3.Size ";

      cout<<"\n4.Display Front element";

      cout<<"\n5.Dispaly Rear element";

      cout<<"\n6.Display All Element";

      cout<<"\n\nEnter your choice : ";

      cin>>ch;

      switch(ch)

      {

case 1:

              if(q.empty())

      {

                  cout<<"\nHow many elements do you want to insert : ";

            cin>>n;

          for(i=0;i<n;i++)

                  {

     cout<<"\nEnter "<<i+1<<" Element :";

     cin>>data;

     q.push(data);

          }

      }

      else

      {

cout<<"\nQueue is Full";

      }

      break;

   

         case 2 :

            if(!q.empty())

            {

                cout<<"\nDeleted element is : "<<q.front();

                q.pop();

            }

            else

            {

                cout<<"\nQueue is Empty...!!!";

            }

              break;

   

case 3 :

     if(!q.empty())

           {

               cout<<"\nSize of Queue is : "<<q.size();     

           }

           else

           {

                 cout<<"\nQueue is Empty...!!!";

           }

           break;

     

       case 4:

            if(!q.empty())

            { 

              cout<<"\nFront element of queue is : "<<q.front();

            }

            else

            {

                cout<<"\nQueue is Empty...!!!";

            }

          break;

    

       case 5:

            if(!q.empty())

          { 

                cout<<"\n Rear element is: "<<q.back();

            }

            else

          {

              cout<<"\nQueue is Empty...!!!";

          }

          break;

     

      case 6:

           cout<<"\nQueue elements are:";

           while(!q.empty())

         { 

            cout<<" "<<q.front();

              q.pop();

           }

           break;

          

      default :

          cout<<"\nPlease Enter valid choice...!!!";

          break;

         

     }

      cout<<"\nDo you want to continue(y/n) : ";

      cin>>ans;

  }

   while(ans=='y'||ans=='Y');

   return 0;

}


                                                  ********** OUTPUT ************

/*       
student@IOE-HWLAB:~$ g++ 9.queue_STL.cpp -o a
student@IOE-HWLAB:~$ ./a

Queue Using STL
1.Insert
2.Delete
3.Size 
4.Display Front element
5.Dispaly Rear element
6.Display All Element

Enter your choice : 1

How many elements do you want to insert : 5

Enter 1 Element :1

Enter 2 Element :2

Enter 3 Element :3

Enter 4 Element :4

Enter 5 Element :5

Do you want to continue(y/n) : y

Queue Using STL
1.Insert
2.Delete
3.Size 
4.Display Front element
5.Dispaly Rear element
6.Display All Element

Enter your choice : 2

Deleted element is : 1
Do you want to continue(y/n) : y

Queue Using STL
1.Insert
2.Delete
3.Size 
4.Display Front element
5.Dispaly Rear element
6.Display All Element

Enter your choice : 3

Size of Queue is : 4
Do you want to continue(y/n) : y

Queue Using STL
1.Insert
2.Delete
3.Size 
4.Display Front element
5.Dispaly Rear element
6.Display All Element

Enter your choice : 4

Front element of queue is : 2
Do you want to continue(y/n) : y

Queue Using STL
1.Insert
2.Delete
3.Size 
4.Display Front element
5.Dispaly Rear element
6.Display All Element

Enter your choice : 5

 Rear element is: 5
Do you want to continue(y/n) : y

Queue Using STL
1.Insert
2.Delete
3.Size 
4.Display Front element
5.Dispaly Rear element
6.Display All Element

Enter your choice : 6

Queue elements are: 2 3 4 5
Do you want to continue(y/n) : n

*/

For more such posts click the link:-http://svencrai.com/G8W

Post a Comment

Previous Post Next Post