Stack
Stack is LIFO (Last in First out) data structure in which elements are inserted and extracted only from one end. Basic functions of the Stack class are as:
push() function pushes the element in the stack.
pop() function pops the element from the stack.
size() returns the size of the stack.
empty() returns 1 if stack is empty and 0 in case if stack is not empty.
top() returns the top element of the stack.
Following code demonstrates the use of the Stack class.
Code
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stack"
using namespace std;
int main()
{
stack<int> S;
S.push(1);
S.push(2);
S.push(3);
cout<<"Size > "<<S.size()<<endl;
cout<<"Is Empty > "<<S.empty()<<endl;
cout<<"Elements poping !!! "<<endl;
while(!S.empty())
{
cout<<S.top()<<endl;
S.pop();
}
cout<<"Elements poped !!! "<<endl;
cout<<"Size > "<<S.size()<<endl;
cout<<"Is Empty > "<<S.empty()<<endl;
_getche();
return 0;
}
Output
Size > 3
Is Empty > 0
Elements poping !!!
3
2
1
Elements poped !!!
Size > 0
Is Empty > 1
0 comments:
Post a Comment