Guest User

Untitled

a guest
May 26th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string.h>
  4. using namespace std;
  5. #pragma warning(disable : 4996)
  6. #define _CRT_SECURE_NO_WARNINGS
  7. class QueueIterator;
  8. class AbstractIterator
  9. {
  10. public:
  11. virtual ~AbstractIterator() {};
  12.  
  13. virtual void Reset() = 0;
  14.  
  15. };
  16. class AbstractContainer
  17. {
  18. public:
  19. virtual ~AbstractContainer() {};
  20. virtual bool IsEmpty() const = 0;
  21. virtual bool IsFull() const = 0;
  22. };
  23. class AbstractQueue : public AbstractContainer
  24. {
  25. public:
  26. virtual void push(int) = 0;
  27. virtual void del() = 0;
  28. };
  29. class ArrayQueue : public AbstractQueue
  30. {
  31. protected:
  32. int size;
  33. int* p;
  34. int head;
  35. int k;
  36. public:
  37. ArrayQueue()
  38. {
  39. size = 0; head = 0; k = 0;
  40. }
  41. ArrayQueue(int _size)
  42. {
  43. size = _size;
  44. p = new int[size];
  45. k = 0;
  46. head = 0;
  47. }
  48. ArrayQueue(ArrayQueue &q)
  49. {
  50. size = q.size;
  51. p = new int[size];
  52. k = q.k;
  53. head = q.head;
  54. }
  55. ~ArrayQueue()
  56. {
  57. delete[]p;
  58. }
  59. void push(int n)
  60. {
  61. if (this->IsFull())
  62. {
  63. cout << "Контейнер заполнен" << endl;
  64. }
  65. else
  66. {
  67. k++;
  68. p[head++] = n;
  69. }
  70. cout << "Элемент добавлен в конец:" <<n<< endl;
  71. }
  72. void del()
  73. {
  74. if (this->IsEmpty())
  75. {
  76. cout << "Контейнер пуст" << endl;
  77. }
  78. else{
  79. cout << "Удалили сверху: " << p[0] << endl;
  80. for (int i = 0; i < head; i++)
  81. p[i] = p[i + 1];
  82. k--;
  83. head--;
  84. }
  85. }
  86. bool IsEmpty()const
  87. {
  88. if (k == 0)
  89. return 1;
  90. else return 0;
  91. }
  92. bool IsFull()const
  93. {
  94. if (k == size)
  95. return 1;
  96. else return 0;
  97. }
  98. void Print()
  99. {
  100. for (int i = 0; i < head; i++)
  101. cout << p[i] << " ";
  102. cout << endl;
  103. }
  104. friend class QueueIterator;
  105. };
  106. class ArrayDequeue : public ArrayQueue
  107. {
  108. public:
  109. ArrayDequeue(int _size) :ArrayQueue(_size){}
  110. ArrayDequeue(const ArrayDequeue &d);
  111. ~ArrayDequeue()
  112. {
  113. }
  114. void pop()
  115. {
  116. if (this->IsEmpty())
  117. {
  118. cout << "Контейнер пуст" << endl;
  119. }
  120. else
  121. {
  122.  
  123. k--;
  124. head--;
  125. cout << "Удалили снизу: " << p[head] << endl;
  126. }
  127. }
  128. void ins(int n)
  129. {
  130. if (this->IsFull())
  131. {
  132. cout << "Контейнер заполнен" << endl; return;
  133. }
  134. for (int i = head; i>0; i--)
  135. p[i] = p[i - 1];
  136. p[0] = n;
  137. head++;
  138. k++;
  139. cout << "\nЭлемент добавлен в начало:" <<n<< endl;
  140. }
  141. };
  142. class QueueIterator : public AbstractIterator
  143. {
  144. ArrayQueue _a;
  145. int pos;
  146. public:
  147. QueueIterator(){ pos = 0; }
  148. QueueIterator(ArrayQueue& a)
  149. {
  150. pos = 0;
  151. _a.head = a.head;
  152. _a.k = a.k;
  153. _a.size = a.size;
  154. _a.p = new int[_a.size];
  155. for (int i = 0; i < _a.head; i++)
  156. _a.p[i] = a.p[i];
  157. }
  158. bool InRange()
  159. {
  160. if (pos >= 0 && pos < _a.head)
  161. return 1;
  162. else return 0;
  163. }
  164. void Reset()
  165. {
  166. pos = 0;
  167. }
  168. int operator *()
  169. {
  170. return _a.p[pos];
  171. }
  172. void operator ++()
  173. {
  174. pos++;
  175. }
  176. ~QueueIterator(){};
  177. };
  178. void Print( ArrayQueue&y)
  179. {
  180. QueueIterator out(y);
  181. while (out.InRange() == 1)
  182. {
  183. cout << *out << " ";
  184. out++;
  185. }
  186. out.Reset();
  187. }
  188. int main()
  189. {
  190. setlocale(LC_ALL, "Russian");
  191. ArrayDequeue A(8);
  192. int value;
  193. for (int i = 0; i < 6; i++)
  194. {
  195. cout << "\nЗначение > "; cin >> value;
  196. A.push(value);
  197. }
  198. Print(A);
  199. A.ins(66);
  200. Print(A);
  201. for (int i = 0; i < 5; i++)
  202. {
  203. A.del();
  204. A.Print();
  205. }
  206. A.pop();
  207. Print(A);
  208. system("pause");
  209. return 0;
  210. }
Add Comment
Please, Sign In to add comment