Container Adaptors

When to use iterative development? You should use iterative development only on projects that you want to succeed. Martin Fowler

stack

Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-17 04:17:30

0 Comments


Definition

Stack is a data structure store and remove the items based on last in and first out. Priority queues are implemented based on container.

Character

Last-in, first-out (LIFO) data structure.
The stack insert into the top and removed from the top.
Stack can be implemented using array or linked list (dynamic memory)
If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state.
If the stack is empty, it goes into underflow state.

source Code

								
#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
	stack < int > st;
	cout << "Store items to stack." << endl;

	for (int i = 0; i < 10;i++)
		st.push(i);
	
	cout << "Stack size : " << st.size() << endl;
	cout << "Display items from stack : " << st.size() << endl;
	while (!st.empty())
	{
		int w = st.top();
		cout << w << " ";
		st.pop();
	}

	cout << endl;

	return 0;
}

						

Output

    					 
					Store items to stack.
					Stack size : 10
					Display items from stack : 10
					9 8 7 6 5 4 3 2 1 0
					    					 
					    

When to use

If you want insert and remove the items using last in first out (LIFO) order, you can use Stack.

0 Comments



Please login to add comments.