Monday, 1 February 2016

Simple Stack Functions Program


#define MAX 10
class stack
{
int a[MAX];
int x;
int top;
public:
stack()
{
x=0;
top=-1;
}
int push(int );
int pop();
int peek();
int empty();
};

int stack::push(int d)
{
if(top==MAX-1)
{
return 0;
}
else
{
top++;
a[top]=d;
return a[top];
}

int stack::pop()
{
int x;
if(top==-1)
{
return 0;
}
else
{
x=a[top];
top--;
return x;
}  
}

int stack::peek()
{
int x;
if(top==-1)
{
return 0;
}
else
{
x=a[top];
return x;
}    
}

int stack::empty()
{
int x;
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}

void main()
{
stack o;
clrscr();
int k,l,p=0,q=0,x,y,z;
y=1;
do
{
switch(y)
{
case 1:
cin>>k;
p=o.push(k);
cout<<"\n\tpush"<<p;
break;

case 2:
x=o.pop();
cout<<"\n\tpop"<<x;
break;

case 3:
x=o.peek();
cout<<"\n\tpeek"<<x;
break;
}

cout<<"enter your choice \n\t1.push \n\t2.pop \n\t3.peek";
cin>>y;}
while(y<=3);

getch();

}

No comments:

Post a comment