C++ Programming-Stack operation

C++ Programming-Stack operation

6 10 99
C++ Programming-Stack operation 10 6 99


#include<iostream.h>
#include<conio.h>
class stack
{
int a[10],top,item,i;
public:
stack()
{
top=-1;
}
void push()
{
if(top==10)
cout<<"stack is full";
else
{
cout<<"enter the elements to be pushed";
cin>>item;
top=top+1;
a[top]=item;
}
}
void pop()
{
if(top==-1)
cout<<"stack is empty";
else
top=top-1;
}
void display()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<"the stack elements are";

for(i=top;i>=0;i--)
{
cout<<""<<a[i];
}
}
}
};
void main()
{
int ch,top;
char ans;
//top=-1;
stack ob;
do
{
cout<<"\n1.push\n2.pop\n3.display";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1:ob.push();
break;
case 2:ob.pop();
break;
case 3:ob.display();
break;
default:cout<<"invalid entry";
}
cout<<"\n do you want to continue:";
cin>>ans;
}while(ans=='y'||ans=='Y');
}

output:



1.push
2.pop
3.display
 enter your choice1
enter the elements to be pushed23

 do you want to continue:y

1.push
2.pop
3.display
 enter your choice1
enter the elements to be pushed45

 do you want to continue:y

1.push
2.pop
3.display
 enter your choice3
the stack elements are4523
 do you want to continue:n











0 comments:

Post a Comment

 
HowTo © 2015 | HowTo | Design By HowZto | HOWTO
Top