Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. char menu()
  4. {
  5. char choice;
  6. cout << "Menu \n";
  7. cout << "1. Add an element \n";
  8. cout << "2. Remove an element \n";
  9. cout << "3. Show the stack \n";
  10. cout << "4. Exit\n";
  11. cin >> choice;
  12. return choice;
  13. }
  14. struct stack {
  15. int data;
  16. struct stack* next;
  17. };
  18. void s_push(stack** top, int D) {
  19. stack* q;
  20. q = new stack();
  21. q->data = D;
  22. if (top == NULL) {
  23. *top = q;
  24. }
  25. else
  26. {
  27. q->next = *top;
  28. *top = q;
  29. }
  30. }
  31. void s_print(stack* top) {
  32. stack* q = top;
  33. if (top == NULL)
  34. {
  35. cout << "The stack is empty \n";
  36. }
  37. else
  38. {
  39. cout << "The stack consists : \n";
  40. while (q) {
  41. cout << q->data << " ";
  42. q = q->next;
  43. }
  44. }
  45. }
  46. void s_delete_key(stack** top) {
  47. stack* q = *top;
  48. *top = q->next;
  49. free(q);
  50. q->data = NULL;
  51. q->next = NULL;
  52. }
  53. int main()
  54. {
  55. stack* top = NULL;
  56. char choice;
  57. int number;
  58. do {
  59. choice = menu();
  60. switch (choice)
  61. {
  62. case '1':
  63. cout << "Input an element :";
  64. cin >> number;
  65. cout << endl;
  66. s_push(&top, number);
  67. break;
  68. case '2':
  69. s_delete_key(&top);
  70. cout << "The top element is deleted \n";
  71. break;
  72. case '3':
  73. s_print(top);
  74. cout << endl;
  75. break;
  76. default: cout << "System exit\n";
  77. }
  78. } while (choice != '4');
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement