fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #define SIZE 100000
  4.  
  5. class Stiva {
  6.  
  7. public:
  8. //constructor of the class
  9. Stiva() {
  10. summit = 0;
  11. container = new struct Node[SIZE];
  12. }
  13.  
  14. //destructor of the class
  15. ~Stiva() {
  16. delete [] container;
  17. }
  18.  
  19. void Push(int value) {
  20.  
  21. if(summit == SIZE ) {
  22. std::cout<<"The stack is full.";
  23. return;
  24. } else {
  25. std::cout<<"Added!\n";
  26. if(summit == 0) {
  27. container[summit] = {value, value};
  28. summit++;
  29. } else {
  30. container[summit] = {value, max(container[summit-1].max_value, value)};
  31. summit++;
  32. }
  33. }
  34. }
  35. int max(int a, int b) {if(a>b) return a; else return b;}
  36. int Top() {
  37. if(!this->isEmpty()) return container[summit-1].value;
  38. return -1;
  39. }
  40.  
  41. int Max() {
  42. if(!this->isEmpty()) return container[summit-1].max_value;
  43. return -1;
  44. }
  45.  
  46. int isEmpty() {
  47. return summit == 0;
  48. }
  49.  
  50. void Pop() {
  51. if(!this->isEmpty()) summit--;
  52. }
  53.  
  54. void display() {
  55. for(int i = 0; i < summit; ++i) std::cout<<container[i].value<<"-"<<container[i].max_value<<"\n";
  56. }
  57. private:
  58. struct Node {
  59. int value,
  60. max_value;
  61. };
  62. struct Node *container;
  63. int summit;
  64. };
  65.  
  66. int main(int argc, char const *argv[]) {
  67.  
  68. Stiva w;
  69. w.Push(4);
  70. w.Push(2);
  71. w.Push(5);
  72. w.Push(1);
  73. std::cout << w.Top() << " " << w.Max() << "\n"; // afiseaza 1 5
  74. w.Pop();
  75. w.Pop();
  76. std::cout << w.Top() << " " << w.Max() << "\n"; // afiseaza 2 4
  77.  
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0.01s 5420KB
stdin
Standard input is empty
stdout
Added!
Added!
Added!
Added!
1 5
2 4