alaminrifat

Dynamic Stack with C++

Nov 2nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int top = -1,s = 10;
  4.  
  5. void push(int *Stk,int n){
  6.     if(top>=s-1)
  7.    {
  8.      cout<<"Stack Overflow"<<endl;
  9.    }
  10.    else
  11.     {
  12.      top++;
  13.      Stk[top]=n;
  14.    }
  15.  
  16. }
  17.  
  18. void pop(int *Stk){
  19.     if(top<=-1)
  20.    {
  21.      cout<<"Stack Underflow"<<endl;
  22.    }
  23.    else
  24.     {
  25.       cout<<"The popped element is "<< Stk[top] <<endl;
  26.       top--;
  27.     }
  28.    }
  29. void display(int *Stk){
  30.     if(top>=0)
  31.     {
  32.       cout<<"Stack elements are: ";
  33.       for(int i=0; i<=top; i++)
  34.       {
  35.       cout<<Stk[i]<<" ";
  36.       }
  37.       cout<<endl;
  38.     }
  39.     else
  40.     {
  41.     cout<<"Stack is empty";
  42.     }
  43.  
  44. }
  45.  
  46. int main(){
  47.     int *Stack;
  48.     Stack = new int[s];
  49.     push(Stack,5);
  50.     push(Stack,10);
  51.     push(Stack,50);
  52.     push(Stack,56);
  53.     pop(Stack);
  54.     display(Stack);
  55.  
  56.    
  57.  
  58.  
  59.  
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment