Advertisement
splash365

stack (using array)

Feb 9th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define SIZE 5
  5. int A[SIZE];
  6. int top = -1;
  7.  
  8. bool isempty()
  9. {
  10.   if(top==-1)
  11.   return true;
  12.   else
  13.   return false;
  14. }
  15.  
  16. void push(int value)
  17. {
  18.   if(top==SIZE-1)
  19.   {    cout<<"Stack is full!\n";
  20.   }
  21.    else
  22.   {
  23.     top++;
  24.     A[top]=value;
  25.   }
  26. }
  27.  
  28. void pop()
  29. {
  30.  if(isempty())
  31.   cout<<"Stack is empty!\n";
  32.  else
  33.   top--;
  34. }
  35.  
  36. void show_top()
  37. {
  38.  if(isempty())
  39.   cout<<"Stack is empty!\n";
  40.  else
  41.   cout<<"Element at top is: "<<A[top]<<"\n";
  42.  
  43. }
  44.  
  45. void displayStack()
  46. {
  47.   if(isempty())
  48.  {
  49.     cout<<"Stack is empty!\n";
  50.  }
  51.  else
  52.  {
  53.   for(int i=0 ; i<=top; i++)
  54.     cout<<A[i]<<" ";
  55.    cout<<"\n";
  56.  
  57.   }
  58.  
  59. }
  60.  
  61. int main()
  62. {
  63.  
  64.  int choice, flag=1, value;
  65.  while( flag == 1)
  66.  {
  67.  cout<<"\n1.PUSH 2.POP 3.SHOW_TOP 4.DISPLAY_STACK 5.EXIT\n";
  68.  cin>>choice; switch (choice)
  69.  {
  70.  case 1: cout<<"Enter Value:\n";
  71.          cin>>value;
  72.          push(value);
  73.          break;
  74.  case 2: pop();
  75.          break;
  76.  case 3: show_top();
  77.          break;
  78.  case 4: displayStack();
  79.          break;
  80.  case 5: flag = 0;
  81.          break;
  82.  }
  83.  }
  84.   return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement