Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int A[5], top = -1;
  6.  
  7. void push(int item)
  8. {
  9. if(top == 4)
  10. {
  11. cout<<"Stack is full"<<endl<<endl;
  12. }
  13. else
  14. {
  15. top = top + 1;
  16. A[top] = item;
  17. }
  18. cout<<"Added number : "<< item << " position : "<< top <<endl;
  19. }
  20.  
  21. void pop()
  22. {
  23. int item;
  24. if (top == - 1)
  25. {
  26. cout<<"Stack is empty"<<endl;
  27. }
  28. else
  29. {
  30. item = A [top];
  31. cout <<"Deleted number is : " << item << " position : "<< top<<endl;
  32. top = top - 1;
  33. }
  34. }
  35.  
  36. void print()
  37. {
  38. if(top == -1)
  39. {
  40. cout<<"Stack is empty !"<<endl;
  41. }
  42. else
  43. {
  44. cout<<"Stack : "<<endl;
  45. //for(int i=4; i>=0;i--)
  46. for(int i=top; i > -1; i--)
  47. {
  48. cout<<A[i]<< "("<<i<<")"<<endl;
  49. }
  50. }
  51. cout<<endl;
  52. }
  53.  
  54. int main()
  55. {
  56. cout<<"Push operation"<<endl;
  57. push(100); print();
  58. push(99); print();
  59. push(88); print();
  60. push(77); print();
  61. push(66);print();
  62. cout<<"Pop operation"<<endl;
  63. pop(); print();
  64. pop(); print();
  65. pop(); print();
  66. pop(); print();
  67. pop(); print();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement