Monday, March 17, 2014

C++ Stack

Stack is another C++ STL container.





















Stack is a dynamic array where you can add elements form the end and remove elements again from the end.
It is declared like this. The stack is just alike dish stacking one on another. When you put some plates on one another then when you need them you need to take them out from the top.
....
#include <stack>
.........
...........
..........
stack<type> s;
type is the data type you want your array to hold.

Here are the main functions of stack.
suppose we have
stack<int> a;

a.empty()
This is a boolean function which returns "true" if the stack is completely empty and "false" otherwise.

a.pop()
This function removes the element which is at the end of the stack.

a.push(int x);
This function will add the element x form the back of our stack.

a.top()
This function returns the value of the last element of stack.

No comments:

Post a Comment