Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int size=100;
- class stack{
- int items[size];
- int top;
- public:
- stack()
- {
- top=-1;
- }
- void push();
- void pop();
- void travers();
- };
- void stack::push()
- {
- int item;
- if (top==size-1)
- cout<<"\n the stack is full";
- else
- {
- cout<<"\n enter the element =";
- cin>>item;
- items[++top]=item;
- }
- }
- void stack::pop()
- {
- int item;
- if (top==-1)
- cout<<"\n the steack is empty";
- else
- {
- item=items[top--];
- cout<<"\n the deleted elemnt is = "<<item;
- }
- }
- void stack::travers()
- {
- int i;
- if (top==-1)
- cout<<"\n the stack is empty ";
- else
- {
- cout<<"\n\n the elemets in the stack are >>";
- for(i=top;i>=0;i--)
- {
- cout<<endl<<items[i];
- }
- }
- }
- int main()
- {
- int choice ;
- char ch;
- stack ps;
- do{
- cout<<"\n1.push ";
- cout<<"\n2.pop";
- cout<<"\n3.traverse ";
- cout<<"\n enter your choice = ";
- cin>>choice;
- switch(choice){
- case 1:
- ps.push();
- break;
- case 2:
- ps.pop();
- break;
- case 3:
- ps.travers();
- break;
- default :
- cout<<"\n you entered wrong choice ";
- }
- cout<<"\n\npress(Y|y) to continue = ";
- cin>>ch;
- }while (ch=='Y'||ch=='y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement