Container Adaptors

Programming can be fun, so can cryptography; however they should not be combined. Kreitzberg and Shneiderman

queue

Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-16 22:44:37

0 Comments


Definition

Queue is a container which store the data in first in first out order. The items always stored in back and removed from front.

Character

It supports FIFO context (first-in first-out) .
Items are pushed into the queue and popped from its queue.
Iterator doesn’t not support.
Common implementations are circular buffers and linked lists.
Queue overflow results from trying to add an element onto a full queue and queue underflow happens when trying to remove an element from an empty queue.

source Code

								
#include "stdafx.h"

#include < queue >
#include < iostream >
using namespace std;

void print(queue < int > &s, int num)
{
	if (!num)
	{
		cout << endl;
		return;
	}
	int x = s.front();
	s.pop();
	cout << x << " ";
	s.push(x);
	print(s, --num);
}

int _tmain(int argc, _TCHAR* argv[])
{
	queue < int > q;

	cout << "Store items from queue " << endl;

	for (int i = 0; i < 10; i++)
		q.push(i);

	cout << "Display items in queue : " ;
	print(q, 10);

	cout << "Size of Queue " << q.size() << "." << endl;

	cout << "Queue last item : " << q.back() << endl;
	cout << "Queue first item : " << q.front() << endl;


	q.pop();
	cout << "After pop queue size " << q.size() << "." << endl;
	
	cout << "Queue last item : " << q.back() << endl;
	cout << "Queue first item : " << q.front() << endl;

	return 0;
}

						

Output

    					 
					Store items from queue
					Display items in queue : 0 1 2 3 4 5 6 7 8 9
					Size of Queue 10.
					Queue last item : 9
					Queue first item : 0
					After pop queue size 9.
					Queue last item : 9
					Queue first item : 1
					    					 
					    

When to use

If you want insert and remove the items using first in first out (FIFO) order, you can use Queue.

0 Comments



Please login to add comments.