Earn without any Investment!

Monday, July 12, 2010

Stack Operation

#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
private:
int top;
int a[10];
public:
void push();
void pop();
void count();
void exit();
stack()
{
top=-1;
}
};
void stack::push()
{
clrscr();
int b;
top++;
if(top<=9)
{
cout<<"Enter the element:";
cin>>b;
a[top]=b;
}
else
cout<<"\nOut of range \n";
}
void stack::pop()
{
clrscr();
if(top<0)
cout<<"Stack is Empty";
else
{
cout<<"The poped element is: "<<a[top]<<"\n";
top--;

}
}
void stack::count()
{
clrscr();
cout<<"The total is: "<<top+1<<"\n";
}
void main()
{
clrscr();
int ch;
stack s;
while(1)
{
cout<<"Menu:\n";
cout<<" 1.push \n";
cout<<" 2.pop \n";
cout<<" 3.count \n";
cout<<" 4.exit \n";
cout<<"Enter u'r choice:";
cin>>ch;
switch(ch)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.count();
break;
case 4:
exit(0);
break;
default:
cout<<"Enter correct choice:";
clrscr();
break;
}
}
}