fork download
  1. #region using
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. #endregion
  7.  
  8. namespace ConsoleApplicationTest
  9. {
  10. public static class Program
  11. {
  12. private static void Main()
  13. {
  14. // Применение
  15. var myQueue = new MyQueue<int>();
  16.  
  17. // Добавляем элементы
  18. myQueue.Enqueue(1);
  19. myQueue.Enqueue(2);
  20. myQueue.Enqueue(3);
  21. myQueue.Enqueue(4);
  22.  
  23. // Преобразуем в массив и печатаем элементы из него.
  24. foreach (int i in myQueue.ToArray())
  25. Console.WriteLine(i);
  26.  
  27. Console.WriteLine();
  28.  
  29. // Извлекаем элементы
  30. while (myQueue.Count > 0)
  31. Console.WriteLine(myQueue.Dequeue());
  32.  
  33. Console.ReadKey();
  34. }
  35. }
  36.  
  37. public class MyQueue<T>
  38. {
  39. // Поля
  40. private T[] _array;
  41. private int _head;
  42. private int _tail;
  43.  
  44. // Методы
  45. /// <summary>
  46. /// Создаём очередь. Ёмкость по умолнчанию - 0;
  47. /// </summary>
  48. public MyQueue()
  49. {
  50. _array = new T[0];
  51. }
  52.  
  53. /// <summary>
  54. /// Создаём очередь на основе коллекции.
  55. /// </summary>
  56. /// <param name="collection">Исходная коллекция.</param>
  57. public MyQueue(IEnumerable<T> collection)
  58. {
  59. if (collection == null)
  60. throw new ArgumentNullException();
  61. _array = new T[4];
  62. Count = 0;
  63. foreach (T variable in collection)
  64. Enqueue(variable);
  65. }
  66.  
  67. /// <summary>
  68. /// Создаём очередь с заданной начальной ёмкостью.
  69. /// Если количество добавленных элементов превысит заданную ёмкость, то она будет автоматически увеличена.
  70. /// </summary>
  71. /// <param name="capacity">Начальная ёмкость.</param>
  72. public MyQueue(int capacity)
  73. {
  74. if (capacity < 0)
  75. throw new ArgumentOutOfRangeException();
  76. _array = new T[capacity];
  77. _head = 0;
  78. _tail = 0;
  79. Count = 0;
  80. }
  81.  
  82. /// <summary>
  83. /// Количество элементов в очереди.
  84. /// </summary>
  85. public int Count { get; private set; }
  86.  
  87. /// <summary>
  88. /// Очистка очереди.
  89. /// </summary>
  90. public void Clear()
  91. {
  92. if (_head < _tail)
  93. Array.Clear(_array, _head, Count);
  94. else
  95. {
  96. Array.Clear(_array, _head, _array.Length - _head);
  97. Array.Clear(_array, 0, _tail);
  98. }
  99. _head = 0;
  100. _tail = 0;
  101. Count = 0;
  102. }
  103.  
  104. /// <summary>
  105. /// Проверка на нахождении элемента в очереди.
  106. /// </summary>
  107. /// <param name="item">Элемент.</param>
  108. /// <returns>true, если элемент содержится в очереди.</returns>
  109. public bool Contains(T item)
  110. {
  111. int index = _head;
  112. int num2 = Count;
  113. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  114. while (num2-- > 0)
  115. {
  116. if (item == null)
  117. {
  118. if (_array[index] == null)
  119. return true;
  120. }
  121. else if ((_array[index] != null) && comparer.Equals(_array[index], item))
  122. return true;
  123. index = (index + 1)%_array.Length;
  124. }
  125. return false;
  126. }
  127.  
  128. /// <summary>
  129. /// Извлечение элемента из очереди.
  130. /// </summary>
  131. /// <returns>Извлечённый элемент.</returns>
  132. public T Dequeue()
  133. {
  134. if (Count == 0)
  135. throw new InvalidOperationException();
  136. T local = _array[_head];
  137. _array[_head] = default(T);
  138. _head = (_head + 1)%_array.Length;
  139. Count--;
  140. return local;
  141. }
  142.  
  143. /// <summary>
  144. /// Добавление элемента в очередь.
  145. /// </summary>
  146. /// <param name="item">Добавляемый элемент.</param>
  147. public void Enqueue(T item)
  148. {
  149. if (Count == _array.Length)
  150. {
  151. var capacity = (int) ((_array.Length*200L)/100L);
  152. if (capacity < (_array.Length + 4))
  153. capacity = _array.Length + 4;
  154. SetCapacity(capacity);
  155. }
  156. _array[_tail] = item;
  157. _tail = (_tail + 1)%_array.Length;
  158. Count++;
  159. }
  160.  
  161. /// <summary>
  162. /// Просмотр элемента на вершине очереди.
  163. /// </summary>
  164. /// <returns>Верхний элемент.</returns>
  165. public T Peek()
  166. {
  167. if (Count == 0)
  168. throw new InvalidOperationException();
  169. return _array[_head];
  170. }
  171.  
  172. // Изменение ёмкости очереди.
  173. private void SetCapacity(int capacity)
  174. {
  175. var destinationArray = new T[capacity];
  176. if (Count > 0)
  177. {
  178. if (_head < _tail)
  179. Array.Copy(_array, _head, destinationArray, 0, Count);
  180. else
  181. {
  182. Array.Copy(_array, _head, destinationArray, 0, _array.Length - _head);
  183. Array.Copy(_array, 0, destinationArray, _array.Length - _head, _tail);
  184. }
  185. }
  186. _array = destinationArray;
  187. _head = 0;
  188. _tail = (Count == capacity) ? 0 : Count;
  189. }
  190.  
  191. /// <summary>
  192. /// Преобразование очереди в массив.
  193. /// </summary>
  194. /// <returns>Массив с элементами из очереди.</returns>
  195. public T[] ToArray()
  196. {
  197. var destinationArray = new T[Count];
  198. if (Count != 0)
  199. {
  200. if (_head < _tail)
  201. {
  202. Array.Copy(_array, _head, destinationArray, 0, Count);
  203. return destinationArray;
  204. }
  205. Array.Copy(_array, _head, destinationArray, 0, _array.Length - _head);
  206. Array.Copy(_array, 0, destinationArray, _array.Length - _head, _tail);
  207. }
  208. return destinationArray;
  209. }
  210. }
  211. }
Success #stdin #stdout 0s 29664KB
stdin
Standard input is empty
stdout
1
2
3
4

1
2
3
4