Advertisement
Guest User

Untitled

a guest
May 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class stackFun
  6. {
  7. private:
  8. int top;
  9. int arr[];
  10. public:
  11. stackFunctions(int n)
  12. {
  13. top = -1;
  14. /*for (int i = 0; i < n; i++)
  15. {
  16. arr[i] = 0;
  17. }*/
  18. }
  19. bool isEmpty()
  20. {
  21. if (top == -1)
  22. {
  23. return true;
  24. }
  25. else
  26. {
  27. return false;
  28. }
  29. }
  30. bool isFull(int &n)
  31. {
  32. if (top == n-1)
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }
  41. void push(int &val, int &n)
  42. {
  43. if(isFull(n))
  44. {
  45. cout<< "Stack is Full";
  46. }
  47. else
  48. {
  49. top += 1;
  50. arr[top] = val;
  51. }
  52. }
  53. int pop()
  54. {
  55. if(isEmpty())
  56. {
  57. cout << "Stack underflow" << endl;
  58. }
  59. else
  60. {
  61. int val = arr[top];
  62. top--;
  63. return val;
  64. }
  65. }
  66. int display(int &n)
  67. {
  68. if (isEmpty())
  69. {
  70. cout << "Stack underflow" << endl;
  71. }
  72. else
  73. {
  74. cout << "The values in the stack are: " << endl;
  75. for (int i = 0; i < n; i++)
  76. {
  77. cout << arr[i] << endl;
  78. }
  79. }
  80. }
  81. int op()
  82. {
  83. cout << endl
  84. << "Enter" << endl
  85. << "1 - for push" << endl
  86. << "2 - for pop" << endl
  87. << "3 - for display" << endl
  88. << "4 - for exit" << endl;
  89. cout << "Choice of Action: ";
  90. }
  91. void dataType(int choice, int val)
  92. {
  93. if (choice == 1)
  94. {
  95. int val;
  96. }
  97. else if (choice == 2)
  98. {
  99. float val;
  100. }
  101. else if (choice == 3)
  102. {
  103. char val;
  104. }
  105. }
  106.  
  107. };
  108.  
  109. int main()
  110. {
  111. stackFun p1;
  112. int choice=0, n=0, val=0;
  113. /*cout << "Enter" << endl
  114. << "1 - for int" << endl
  115. << "2 - for float" << endl
  116. << "3 - for char" << endl;
  117. cout << "Choice: ";
  118. cin >> choice;
  119. cout << endl;
  120. p1.dataType(choice, val); */
  121. cout << "Enter the number of elements in Stack:" << endl;
  122. cin >> n;
  123. p1.pop();
  124. while (choice != 4 || choice > 4){
  125. p1.op();
  126. cin >> choice;
  127. if (choice == 1)
  128. {
  129. cout << "Enter Value: ";
  130. cin >> val;
  131. p1.push(val, n);
  132. }
  133. if (choice == 2)
  134. {
  135. p1.pop();
  136. }
  137. if (choice == 3)
  138. {
  139. p1.display(n);
  140. }
  141. if (choice == 4)
  142. {
  143. cout << "Closing the program..." << endl;
  144. exit(0);
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement