fork download
  1. #include <iostream>
  2. #ifndef STACK_H_
  3. #define STACK_H_
  4.  
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <cassert>
  8.  
  9. using namespace std;
  10.  
  11. template <class t>
  12. struct node{
  13. t data;
  14. node<t>* next;
  15. };
  16.  
  17. template <class t>
  18. class stack
  19. {
  20. public:
  21. stack();
  22. ~stack();
  23. bool isEmpty(){ return (top_ptr=NULL);};
  24. void push(const t&);
  25. void pop();
  26. t top() const;
  27. void reverse();
  28. void clear();
  29. void print();
  30. private:
  31. node<t>* top_ptr;
  32. };
  33.  
  34. template <class t>
  35. stack<t>::stack()
  36. {
  37. top_ptr=NULL;
  38. }
  39.  
  40. template <class t>
  41. stack<t>::~stack()
  42. {
  43. while(top_ptr != NULL) pop();
  44. }
  45.  
  46. template <class t>
  47. void stack<t>::push(const t& source)
  48. {
  49. node<t>* new_node = new node<t>;
  50. new_node->data = source;
  51. new_node->next = top_ptr;
  52. top_ptr = new_node;
  53. cout << "Inserito!" << endl;
  54. }
  55.  
  56. template <class t>
  57. void stack<t>::pop()
  58. {
  59. node<t>* remove = top_ptr;
  60. top_ptr = top_ptr->next;
  61. delete remove;
  62. cout << "Rimosso!" << endl;
  63. }
  64.  
  65. template <class t>
  66. t stack<t>::top() const
  67. {
  68. assert(top_ptr != NULL);
  69. return top_ptr->data;
  70. }
  71.  
  72. template <class t>
  73. void stack<t>::clear()
  74. {
  75. node<t>* temp;
  76. while(top_ptr != NULL)
  77. {
  78. temp = top_ptr;
  79. top_ptr = top_ptr->next;
  80. delete temp;
  81. }
  82. cout << "Clear completato!" << endl;
  83. }
  84.  
  85. template <class t>
  86. void stack<t>::reverse()
  87. {
  88. stack<t> new_stack;
  89. while(top_ptr != NULL)
  90. {
  91. new_stack.push(top_ptr->data);
  92. pop();
  93. }
  94. cout << "Reverse completato!" << endl;
  95. }
  96.  
  97. template <class t>
  98. void stack<t>::print()
  99. {
  100. node<t>* ptr = top_ptr;
  101. while(ptr!=NULL)
  102. {
  103. cout << " " << ptr->data << endl;
  104. ptr = ptr->next;
  105. }
  106. }
  107.  
  108. #endif
  109.  
  110. int main() {
  111. stack<int> stackino;
  112. for(int i = 0; i<10; i++) stackino.push(i);
  113. stackino.pop();
  114. cout << "top(): " << stackino.top() << endl;
  115. stackino.print();
  116. cout << "Invoco clear()" << endl;
  117. stackino.clear();
  118. cout << "Stackino dopo clear():" << endl;
  119. stackino.print();
  120. cout << "Invoco reverse()" << endl;
  121. stackino.reverse();
  122. cout << "Stackino dopo reverse()" << endl;
  123. stackino.print();
  124.  
  125. cout << "FINE!" << endl;
  126. return 0;
  127. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Rimosso!
top(): 8
 8
 7
 6
 5
 4
 3
 2
 1
 0
Invoco clear()
Clear completato!
Stackino dopo clear():
Invoco reverse()
Reverse completato!
Stackino dopo reverse()
FINE!