Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Write a program for a stack as an Array.
- #include<iostream.h>
- using namespace std;
- #define size 20
- void push(int X[], int &t,int);
- int pop(int X[], int &t);
- void show(int X[], int t);
- void main()
- {
- int ch, X[size],n;
- int top=-1;
- do
- {
- cout<<endl<<"Choose any of the following options:"<<endl;
- cout<<"1.Push"<<endl;
- cout<<"2.Pop"<<endl;
- cout<<"3.Show"<<endl;
- cout<<"4.Exit"<<endl;
- cout<<"Enter the choice:"<<endl;
- cin>>ch;
- switch(ch)
- {
- case 1: cout<<"Enter the number to insert:";
- cin>>n;
- push(X,top,n);
- break;
- case 2: cout<<pop(X,top);
- break;
- case 3:
- show(X,top);
- break;
- }
- } while(ch!=4);
- }
- void push(int X[], int& t, int num)
- {
- if(t==size-1)
- cout<<"Stack is overflow";
- else
- {
- t++;
- A[t]=num;
- }
- }
- int pop(int A[], int& t)
- {
- int a;
- if(t==-1)
- cout<<"Stack is underflow";
- else
- {
- a=X[t];
- t--;
- }
- return a;
- }
- void show(int X[], int t)
- {
- if(t==-1)
- cout<<"Can't show";
- else
- {
- for(int i=t;i>=0;i--)
- cout<<X[i]<<" ";
- }
- }
- /*
- Menu:
- 1.Push
- 2.Pop
- 3.Show
- 4.Exit
- Enter the choice:
- 1
- Enter the number to insert:
- 13
- Menu:
- 1.Push
- 2.Pop
- 3.Show
- 4.Exit
- Enter the choice:
- 1
- Enter the number to insert:
- 41
- Menu:
- 1.Push
- 2.Pop
- 3.Show
- 4.Exit
- Enter the choice:
- 2
- 13
- Menu:
- 1.Push
- 2.Pop
- 3.Show
- 4.Exit
- Enter the choice:
- 3
- 41
- Menu:
- 1.Push
- 2.Pop
- 3.Show
- 4.Exit
- Enter the choice:
- 4
- */
Add Comment
Please, Sign In to add comment