arox14

Stacks as an Array

Dec 11th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. Write a program for a stack as an Array.
  2.  
  3. #include<iostream.h>
  4. using namespace std;
  5. #define size 20
  6.  
  7. void push(int X[], int &t,int);
  8. int pop(int X[], int &t);
  9. void show(int X[], int t);
  10.  
  11. void main()
  12. {
  13. int ch, X[size],n;
  14. int top=-1;
  15. do
  16. {
  17. cout<<endl<<"Choose any of the following options:"<<endl;
  18. cout<<"1.Push"<<endl;
  19. cout<<"2.Pop"<<endl;
  20. cout<<"3.Show"<<endl;
  21. cout<<"4.Exit"<<endl;
  22. cout<<"Enter the choice:"<<endl;
  23. cin>>ch;
  24.  switch(ch)
  25.  {
  26.   case 1: cout<<"Enter the number to insert:";
  27.       cin>>n;
  28.       push(X,top,n);
  29.       break;
  30.   case 2: cout<<pop(X,top);
  31.       break;
  32.   case 3:
  33.       show(X,top);
  34.       break;
  35.  }
  36. } while(ch!=4);
  37. }
  38.  
  39. void push(int X[], int& t, int num)
  40. {
  41.  if(t==size-1)
  42.     cout<<"Stack is overflow";
  43.  else
  44.    {
  45.     t++;
  46.     A[t]=num;
  47.  }
  48. }
  49. int pop(int A[], int& t)
  50. {
  51. int a;
  52.  if(t==-1)
  53.    cout<<"Stack is underflow";
  54.  else
  55.    {
  56.      a=X[t];
  57.      t--;
  58.    }
  59.  return a;
  60. }
  61. void show(int X[], int t)
  62. {
  63.  if(t==-1)
  64.    cout<<"Can't show";
  65.  else
  66.   {
  67.     for(int i=t;i>=0;i--)
  68.     cout<<X[i]<<" ";
  69.   }
  70. }
  71.  
  72. /*
  73. Menu:
  74. 1.Push
  75. 2.Pop
  76. 3.Show
  77. 4.Exit
  78. Enter the choice:
  79. 1
  80. Enter the number to insert:
  81. 13
  82.  
  83. Menu:
  84. 1.Push
  85. 2.Pop
  86. 3.Show
  87. 4.Exit
  88. Enter the choice:
  89. 1
  90. Enter the number to insert:
  91. 41
  92.  
  93. Menu:
  94. 1.Push
  95. 2.Pop
  96. 3.Show
  97. 4.Exit
  98. Enter the choice:
  99. 2
  100. 13
  101.  
  102. Menu:
  103. 1.Push
  104. 2.Pop
  105. 3.Show
  106. 4.Exit
  107. Enter the choice:
  108. 3
  109. 41
  110.  
  111. Menu:
  112. 1.Push
  113. 2.Pop
  114. 3.Show
  115. 4.Exit
  116. Enter the choice:
  117. 4
  118. */
Add Comment
Please, Sign In to add comment