Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class MyStackArray
  5. {
  6. private:
  7. int *DATA;
  8. int count;
  9. int capacity;
  10.  
  11. public:
  12. MyStackArray() {
  13. DATA = new int[3];
  14. count = 0;
  15. capacity = 3;
  16. }
  17.  
  18. int *getData() {
  19. return DATA;
  20. }
  21.  
  22. int getCount() {
  23. return count;
  24. }
  25.  
  26. int getCapacity() {
  27. return capacity;
  28. }
  29.  
  30. void setData(int *data) {
  31. DATA = data;
  32. }
  33.  
  34. void setCount(int c) {
  35. count = c;
  36. }
  37.  
  38. void setCapacity(int cap) {
  39. capacity = capacity;
  40. }
  41.  
  42. bool isFull()
  43. {
  44. return count == capacity;
  45. }
  46.  
  47. void push(int item)
  48. {
  49. if(!isFull())
  50. DATA[count++] = item;
  51. else
  52. cout << "Stack is FULL...\n";
  53. }
  54.  
  55. bool isEmpty()
  56. {
  57. return count==0;
  58. }
  59. void pop()
  60. {
  61. if (!isEmpty())
  62. count--;
  63. else
  64. cout << "Stack is Empty!";
  65. }
  66. int peek()
  67. {
  68. return DATA[count-1];
  69. }
  70.  
  71. void display()
  72. {
  73. cout << "\nS[";
  74. for (int i = 0; i < count; i++)
  75. cout << " " << DATA[i];
  76. cout << "]" << endl;
  77.  
  78. }
  79.  
  80. };
  81.  
  82. int main(int argc, char** argv) {
  83. MyStackArray ms;
  84.  
  85. ms.push(4);
  86. ms.push(7);
  87. ms.push(10);
  88. ms.push(20);
  89. ms.display();
  90.  
  91. cout << "\nNaa sa babaw = " << ms.peek() << endl;
  92.  
  93. ms.pop();
  94. ms.pop();
  95. ms.pop();
  96. ms.pop();
  97.  
  98. ms.display();
  99.  
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement