top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How one can implement Queue using STL in C++?

+5 votes
515 views
How one can implement Queue using STL in C++?
posted Jun 10, 2016 by Shahsikant Dwivedi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Queues are probably the most basic STL container. They don't really have too many commands, and they aren't very flexible. You can probably guess where they get their name from... think of a queue in a shop, they do about the same thing.

Starting our program

First off, we need to include the right stuff. All we need to include is #include and be using std::queue; but I'm gonna be using #include just so that you can see what's happening.

#include <iostream>
#include <queue>

using namespace std;

int main ()
{

Creating a queue and adding values

Very simple to create, pretty much the same as any other STL creation. Also note that we use push() to add something to the queue... this does pretty much the same thing as push_back() would in a vector:

queue <string> names; /* Declare a queue */
names.push ("Danny"); /* Add some values to the queue */
names.push ("Kayleigh"); /* Much like vectors */
names.push ("Joe"); /* This basically does the same thing */

Accessing the stuff in a queue

Really simple yet again, all we do in this next bit is make calls to 3 function, all of which are self explanatory:

size() returns the size of the queue (how many members are in it)
front() accesses the front element of the queue (the first one added)
back() accesses the back element of the queue (the last one added)

Notice the similarities to a queue in a shop? Basically, queue follows a "first come, first serve" pattern.

cout << "There are currently " << names.size () << " people in the queue" << endl
     << "The person at the front of the queue is " << names.front () << endl
     << "The person at the back of the queue is " << names.back () << endl << endl;

Removing an element from a queue

Now, remember how I said it follows a "first come, first serve" rule? This is where you see it in action. The front member in the queue (currently me, Danny) is going to be first out of the queue, right? This is simple enough too:

cout << names.front () << " has been served!" << endl;
names.pop ();
cout << "There are currently " << names.size () << " people in the queue" << endl
     << "The person at the front of the queue is " << names.front () << endl
     << names.back () << " is still at the back!" << endl;

Now, Danny (me) has been served and left the queue, so Kayleigh is now at the front, and Joe is still at the back.

Oh, and don't forget to close off main: }

And that's all there is to it! Told you it was simple!

Note: There is no such thing as an iterator when working with queues.

Here is all of the code

#include <iostream>
#include <queue>

using namespace std;

int main ()
{
    queue <string> names; /* Declare a queue */
    names.push ("Danny"); /* Add some values to the queue */
    names.push ("Kayleigh"); /* Much like vectors */
    names.push ("Joe"); /* This basically does the same thing */

    cout << "There are currently " << names.size () << " people in the queue" << endl
         << "The person at the front of the queue is " << names.front () << endl
         << "The person at the back of the queue is " << names.back () << endl << endl;

    cout << names.front () << " has been served!" << endl;
    names.pop ();
    cout << "There are currently " << names.size () << " people in the queue" << endl
         << "The person at the front of the queue is " << names.front () << endl
         << names.back () << " is still at the back!" << endl;
    cin.get ();
    return EXIT_SUCCESS;
}

Example 2:

This C++ Program demonstrates implementation of Queue in STL.
Here is source code of the C++ Program to demonstrate Queue in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C++ Program to Implement Queue in Stl
 */
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    queue<int> q;
    int choice, item;
    while (1)
    {
        cout<<"\n---------------------"<<endl;
        cout<<"Queue Implementation in Stl"<<endl;
        cout<<"\n---------------------"<<endl;
        cout<<"1.Insert Element into the Queue"<<endl;
        cout<<"2.Delete Element from the Queue"<<endl;
    cout<<"3.Size of the Queue"<<endl;
        cout<<"4.Front Element of the Queue"<<endl;
        cout<<"5.Last Element of the Queue"<<endl;
        cout<<"6.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            q.push(item);
            break;
        case 2:
            item = q.front();
            q.pop();
            cout<<"Element "<<item<<" Deleted"<<endl;
            break;
        case 3:
        cout<<"Size of the Queue: ";
        cout<<q.size()<<endl;
            break;
        case 4:
            cout<<"Front Element of the Queue: ";
        cout<<q.front()<<endl;
            break;
        case 5:
            cout<<"Back Element of the Queue: ";
            cout<<q.back()<<endl;
            break;
        case 6:
            exit(1);
        break;
        default:
            cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

OUTPUT

$ g++ queue.cpp
$ a.out
---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 1
Enter value to be inserted: 9

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 1 
Enter value to be inserted: 8

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 1
Enter value to be inserted: 7

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 1
Enter value to be inserted: 6

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 1
Enter value to be inserted: 5

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 1
Enter value to be inserted: 4

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 3
Size of the Queue: 6

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 4
Front Element of the Queue: 9

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 5
Back Element of the Queue: 4

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 2
Element 9 Deleted

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 3
Size of the Queue: 5

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 4
Front Element of the Queue: 8

---------------------
Queue Implementation in Stl

---------------------
1.Insert Element into the Queue
2.Delete Element from the Queue
3.Size of the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 6


------------------
(program exited with code: 1)
Press return to continue
answer Jun 10, 2016 by Manikandan J
...