Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define max 5
  3. using namespace std;
  4. int pop(int *top,int *data);
  5. int push(int *top,int *data);
  6. int stck[5];
  7. int main(){
  8. int top,option,reply,data;
  9. top=-1;
  10.  
  11.     while(1){
  12.         cout<<"1.Push"<<endl;
  13.         cout<<"2.Pop"<<endl;
  14.         cout<<"3.Break"<<endl;
  15.         cin>>option;
  16.         switch (option){
  17.     case 1:
  18.         cout<<"Enter a value: ";
  19.         cin>>data;
  20.         reply=push(&top ,&data);
  21.         if(reply==-1){
  22.             cout<<"Overflow / stack if full"<<endl;
  23.         }else{
  24.         cout<<"Pushed value is "<<data<<endl;
  25.         }
  26.         break;
  27.     case 2:
  28.         cout<<"Enter a value: ";
  29.         cin>>data;
  30.         reply=pop(&top ,&data);
  31.         if(reply==-1){
  32.             cout<<"Underflow / stack if Empty"<<endl;
  33.         }else{
  34.         cout<<"Poped value is "<<data<<endl;
  35.         }
  36.         break;
  37.     case 3:
  38.         return 0;
  39.  
  40.         }
  41.  
  42.     }
  43.  
  44. return 0;
  45. }
  46.  
  47. int push(int *top,int *data){
  48.     if(*top==-1)
  49.         return (-1);
  50.     else{
  51.         *top=*top+1;
  52.         stck[*top]=*data;
  53.         return (1);
  54.     }
  55. }
  56. int pop(int *top,int *data){
  57.     if(*top==-1)
  58.         return (-1);
  59.     else{
  60.         *data=stck[*top];
  61.         *top=*top-1;
  62.         return (1);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement