Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. template<typename T>
  6.  
  7. class Stack {
  8. int size;
  9. int current;
  10. T* container;
  11.  
  12. void copy(T* _container, int _size, int _current) {
  13. size = _size;
  14. current = _current;
  15. container = new T[size];
  16.  
  17. for (int i = 0; i<current; i++)
  18. container[i] = _container[i];
  19. }
  20.  
  21. void erase() {
  22. delete[] container;
  23. }
  24.  
  25. void resize() {
  26. T* helper;
  27. helper = new int[size];
  28.  
  29. for (int i = 0; i<current; i++)
  30. helper[i] = container[i];
  31.  
  32. erase();
  33. copy(helper, 2 * size, current);
  34.  
  35. delete[] helper;
  36. }
  37.  
  38. public:
  39.  
  40. Stack() {
  41. size = 10;
  42. current = 0;
  43. container = new int[size];
  44. }
  45.  
  46. Stack(Stack const &other) {
  47. copy(other.container, other.size, other.current);
  48. }
  49.  
  50. Stack& operator=(Stack const &other) {
  51. if (this != &other) {
  52. erase();
  53. copy(other.container, other.size, other.current);
  54. }
  55. return *this;
  56. }
  57.  
  58. ~Stack() {
  59. erase();
  60. }
  61.  
  62. bool empty() {
  63. if (current == 0) return true;
  64. else return false;
  65. }
  66.  
  67. int getsize() {
  68. return current;
  69. }
  70.  
  71. void push(int item) {
  72. if (size == current) resize();
  73. container[current] = item;
  74. current++;
  75. }
  76.  
  77. void pop() {
  78. current--;
  79. }
  80.  
  81. int top() {
  82. return container[current - 1];
  83. }
  84.  
  85. void swap(int i1, int i2) {
  86. if (i1 >= 0 && i2 >= 0 &&
  87. i1<current &&i2<current) {
  88.  
  89. int helper = container[i1];
  90. container[i1] = container[i2];
  91. container[i2] = helper;
  92. }
  93. }
  94. };
  95.  
  96. class Queue {
  97. int size;
  98. int current;
  99. int* container;
  100.  
  101. void copy(int* _container, int _size, int _current) {
  102. size = _size;
  103. current = _current;
  104. container = new int[size];
  105.  
  106. for (int i = 0; i<current; i++)
  107. container[i] = _container[i];
  108. }
  109.  
  110. void erase() {
  111. delete[] container;
  112. }
  113.  
  114. void resize() {
  115. int* helper;
  116. helper = new int[size];
  117.  
  118. for (int i = 0; i<current; i++)
  119. helper[i] = container[i];
  120.  
  121. erase();
  122. copy(helper, 2 * size, current);
  123.  
  124. delete[] helper;
  125. }
  126.  
  127. public:
  128.  
  129. Stack() {
  130. size = 10;
  131. current = 0;
  132. container = new int[size];
  133. }
  134.  
  135. Stack(Stack const &other) {
  136. copy(other.container, other.size, other.current);
  137. }
  138.  
  139. Stack& operator=(Stack const &other) {
  140. if (this != &other) {
  141. erase();
  142. copy(other.container, other.size, other.current);
  143. }
  144. return *this;
  145. }
  146.  
  147. ~Stack() {
  148. erase();
  149. }
  150.  
  151. bool empty() {
  152. if (current == 0) return true;
  153. else return false;
  154. }
  155.  
  156. int getsize() {
  157. return current;
  158. }
  159.  
  160. void push(int item) {
  161. if (size == current) resize();
  162. container[current] = item;
  163. current++;
  164. }
  165.  
  166. void pop() {
  167. for (int i = 0; i<current - 1; i++) {
  168. container[i] = container[i + 1];
  169. }
  170. current--;
  171. }
  172.  
  173.  
  174. int top() {
  175. return container[current - 1];
  176. }
  177.  
  178. void swap(int i1, int i2) {
  179. if (i1 >= 0 && i2 >= 0 &&
  180. i1<current &&i2<current) {
  181.  
  182. int helper = container[i1];
  183. container[i1] = container[i2];
  184. container[i2] = helper;
  185. }
  186. }
  187. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement