Advertisement
vkichukova

Untitled

Oct 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int MAX = 100;
  6.  
  7. template<class T>
  8. class Queue
  9. {
  10. int head;
  11. int tail;
  12. int itemCount; // *
  13. T * array;
  14.  
  15. void copyQueue(const Queue<T>& other) // *
  16. {
  17. head = other.head;
  18. tail = other.tail;
  19. itemCount = other.itemCount;
  20. array = new T[MAX];
  21. for(int i = head; i <= tail; i++)
  22. array[i] = other.array[i];
  23. }
  24.  
  25. void removeQueue()
  26. {
  27. delete[] array;
  28. }
  29. public:
  30. Queue<T>() //default constructor
  31. {
  32. head = 0;
  33. tail = -1; // *
  34. itemCount = 0;
  35. array = new T[MAX];
  36. }
  37.  
  38. Queue<T>(const Queue<T>& other) //copy constructor
  39. {
  40. copyQueue(other);
  41. }
  42.  
  43. Queue<T>& operator=(const Queue<T>& other) //copy assignment operator
  44. {
  45. if(this != &other)
  46. {
  47. removeQueue();
  48. copyQueue(other);
  49. }
  50. return *this;
  51. }
  52.  
  53. ~Queue<T>() //destructor
  54. {
  55. removeQueue();
  56. }
  57.  
  58. bool isEmpty() const
  59. {
  60. return itemCount == 0; // *
  61. }
  62.  
  63. bool isFull() const
  64. {
  65. return itemCount == MAX; // *
  66. }
  67.  
  68. int size() const
  69. {
  70. return itemCount; // *
  71. }
  72.  
  73. T front() const
  74. {
  75. if(!isEmpty())
  76. return array[head];
  77. else
  78. cout << "The queue is empty!";
  79. }
  80.  
  81. T back() const
  82. {
  83. if(!isEmpty())
  84. return array[tail];
  85. else
  86. cout << "The queue is empty!";
  87. }
  88.  
  89. void pop()
  90. {
  91. if(!isEmpty())
  92. {
  93. head++;
  94. itemCount--;
  95. }
  96. else
  97. cout << "The queue is empty!";
  98. }
  99.  
  100. void push(T element)
  101. {
  102. if(!isFull())
  103. {
  104. array[++tail] = element; // *
  105. itemCount++; // *
  106. }
  107. else
  108. cout << "The queue is full!";
  109. }
  110.  
  111. void swap(int first, int second)
  112. {
  113. if(first!=second && first>=0 && second>=0)
  114. {
  115. T temp = array[head+first]; //*
  116. array[head+first] = array[head+second]; //*
  117. array[head+second] = temp; //*
  118. }
  119. }
  120. void swapQueue(Queue<T>& other)
  121. {
  122. Queue temp = *this;
  123. *this = other;
  124. other = temp;
  125. temp.removeQueue();
  126. }
  127.  
  128. friend ostream& operator<<(ostream& os, const Queue<T>& q)
  129. {
  130. for (int i = q.head; i <= q.tail; i++)
  131. {
  132. os << q.array[i] << " ";
  133. }
  134. return os;
  135. }
  136.  
  137. };
  138.  
  139. int main()
  140. {
  141. Queue<int> q1;
  142. for (int i = 0; i < 10; i++)
  143. {
  144. q1.push(i); // ?
  145. }
  146. cout << q1 << endl;
  147. for (int i = 0; i < 5; i++)
  148. {
  149. q1.pop();
  150. }
  151. cout << q1 << endl;
  152.  
  153. // test copy constructor
  154. Queue<int> q2(q1), q3;
  155. cout << q2 << endl;
  156.  
  157. // test operator=
  158. q3 = q1;
  159. cout << q3 << endl;
  160.  
  161. q1.swap(1,2);
  162. cout << "Swap: " << q1 << endl;
  163. // !!! q1.push(94);
  164. q1.swapQueue(q2);
  165. cout << "Q1 " << q1 << " Q2: " << q2 << endl;
  166.  
  167.  
  168. Queue<char> c1;
  169. for (char i = 'a'; i <= 'z'; i++)
  170. {
  171. c1.push(i);
  172. }
  173. cout << c1 << endl;
  174.  
  175. Queue<char> c2(c1),c3;
  176. cout << c1 << endl;
  177. cout << c3 << endl;
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement