Tuesday, July 19, 2011

Queue and its Implementation using C++

Queue:
 A queue is a container that implements the first in first out protocol. That means that the only accessible object in the container is the one among them that was inserted first. A good analogy is a group of people waiting in line of a ticket. The next one admitted is the person in the line who got here ahead of everyone else.  


Common operations of queue on C++ are
bool empty()
Returns True if the queue is empty, and False otherwise.
T & front()
Returns a reference to the value at the front of a non-empty queue. There is also a constant version of this function, const T & front().
void pop()
Removes the item at the front of a non-empty queue.
void push(const T &foo)
Inserts the argument foo at the back of the queue.
size_type size()
Returns the total number of elements in the queue.


Example: Stuck Implementation on C++ 

#include<iostream>

#include<queue>
using namespace std;
template <class T> void print (const queue <T> &);

int main()

{

queue<string>q;

q.push("Jakir");

q.push("Ruble");

q.pop();

q.push("Mehedi");

q.push("Rimon");

q.pop();
}
template<class T> void print (const queue <T>&q)
{
queue<T> qq=q;

cout <<"size="<< qq.size();

if (qq.empty()) cout<< ";

the queue is empty.";

else    {
cout <<";

front=" << qq.front()        << ", back=" << qq.front ();

<< ": (" << qq.front();

qq.pop();

while(!qq.empty())

{
cout << "," << qq.front();

qq.pop();
}        cout <<").";

}

cout <<"\n";

}

Thank you.

0 comments em “Queue and its Implementation using C++”

Post a Comment

Give your valuable Comment...

 

Programming, Data Structures & Algorithms Copyright © 2011 -- Template created by JAK -- Powered by Blogger