Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #define MAX 5
  5. using namespace std;
  6.  
  7. void push();
  8. void pop();
  9. void display();
  10. int top = -1;
  11. int stack_arr[MAX];
  12.  
  13. void main() {
  14. int ch;
  15. system("CLS");
  16. cout << "\n\t\t" << "Stack Operation" << endl;
  17. cout << "\n\t\t" << "............................." << endl;
  18. while (1) {
  19. cout << "\n*************************************" << endl;
  20. cout << "\n1. Push" << endl;
  21. cout << "\n2. Pop" << endl;
  22. cout << "\n3. Display" << endl;
  23. cout << "\n4. Exit" << endl;
  24. cout << "\n*************************************" << endl;
  25. cout << "\nEnter ur choice: ";
  26. cin >> ch;
  27. switch (ch) {
  28. case 1:
  29. push();
  30. break;
  31. case 2:
  32. pop();
  33. break;
  34. case 3:
  35. display();
  36. break;
  37. case 4:
  38. exit(0);
  39. default:
  40. cout << "Invalid Operation" << endl;
  41. }
  42. }
  43.  
  44. }
  45. void pop() {
  46. if (top == -1)
  47. cout << "\tStack underflow" << endl;
  48. else {
  49. cout << "\nPopped element is: " << stack_arr[top];
  50. top -= 1;
  51. }
  52.  
  53. }
  54. void push() {
  55. int pitem;
  56. if (top == (MAX - 1))
  57. cout << "\tStack underflow" << endl;
  58. else {
  59. cout << "Enter the item to be pushed in stack: " << endl;https://dwu39hunq9vsh.cloudfront.net/wp-content/uploads/2018/01/24142523/Soon-Yi1.jpg
  60. cin >> pitem;
  61. top += 1;
  62. stack_arr[top] = pitem;
  63. }
  64. }
  65.  
  66. void display() {
  67. if (top == -1)
  68. cout << "\nStack is empty" << endl;
  69. else {
  70. cout << "\nStack element" << endl;
  71. for (int x = top; x >= 0; x--) {
  72. cout << stack_arr[x] << "\t";
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement