Wednesday, March 12, 2014

variable size STACK in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class stack
{
public:
int top,size,a[10];
void push(int b);
int pop();
int isfull();
int isempty();
stack (int c)
{
size=c;
top=-1;
}
stack()
{
size=0;
}
};

void stack::push(int b)
{

if(isfull())
cout<<"stack is full";
else
a[++top]=b;
}int stack::pop()
{
if(isempty())
cout<<"stack is empty";
else
{top--;
return(a[top+1]);
}
}
int stack :: isfull()
{
if(top>=size)
return 1;
else return 0;
}
int stack::isempty()
{if(top<=-1)
return 1;
else return 0;

}
int main()
{
int n,c,b[10],i=0;
cout<<"stack size";
cin>>c;
char ch;
stack s(c);
cout<<"enter the exp";
do
{
cout<<"enter";
cin>>n;
s.push(n);
cout<<"do you want to enter more";
cin>>ch;
}while(ch=='y'||ch=='Y');
i=0;
cout<<"in reverse order";
while(!s.isempty())
{
cout<<s.pop();
}
}
 

No comments:

Post a Comment

Contributors

Translate