Advertisement
kot025

lab 11 - Queue.cs

Jun 12th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using ConsoleApplication1;
  7.  
  8. namespace ConsoleApplication11
  9. {
  10. class Queue: ICloneable
  11. {
  12. #region Fields
  13.  
  14. int _capacity;
  15. int _count;
  16. Item _firstItem;
  17. Item _lastItem;
  18.  
  19. #endregion Fields
  20. #region Properties
  21.  
  22. public int Count
  23. {
  24. get { return _count; }
  25. set
  26. {
  27. if (value < 0)
  28. {
  29. _count = 0;
  30. }
  31. else
  32. {
  33. _count = value;
  34. }
  35. }
  36. }
  37. public int Capacity
  38. {
  39. get
  40. {
  41. return _capacity;
  42. }
  43. set
  44. {
  45. if (value < 0)
  46. {
  47. _capacity = 0;
  48. }
  49. else
  50. {
  51. _capacity = value;
  52. }
  53. }
  54. }
  55. public Item FirstItem
  56. {
  57. get { return _firstItem; }
  58. set { _firstItem = value; }
  59. }
  60. public Item LastItem
  61. {
  62. get { return _lastItem; }
  63. set { _lastItem = value; }
  64. }
  65.  
  66. #endregion Properties
  67. #region Constructors
  68.  
  69. public Queue()
  70. {
  71. Capacity = 100;
  72. Count = 0;
  73. FirstItem = null;
  74. LastItem = null;
  75. }
  76. public Queue(int capacity)
  77. {
  78. Capacity = capacity;
  79. Count = 0;
  80. FirstItem = null;
  81. LastItem = null;
  82. }
  83. public Queue(Queue prototypeQueue)
  84. {
  85. Capacity = prototypeQueue.Capacity;
  86. Count = prototypeQueue.Count;
  87. FirstItem = prototypeQueue.FirstItem;
  88. LastItem = prototypeQueue.LastItem;
  89. }
  90.  
  91. #endregion Constructors
  92. #region Methods
  93.  
  94. public bool Contains(object requestedItem)
  95. {
  96. if (Count > 0)
  97. {
  98. Item currentItem = FirstItem;
  99. for (int i = 0; i < Count; i++)
  100. {
  101. if (currentItem == requestedItem)
  102. {
  103. return true;
  104. }
  105. else
  106. {
  107. currentItem = currentItem.Next;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. public void Clear()
  114. {
  115. Count = 0;
  116. }
  117. public void Dequeue()
  118. {
  119. if (Count != 0)
  120. {
  121. FirstItem = FirstItem.Next;
  122. Count--;
  123. }
  124. }
  125. public void Enqueue(object data)
  126. {
  127. if (Count < Capacity)
  128. {
  129. if (Count == 0)
  130. {
  131. FirstItem = new Item(data);
  132. LastItem = FirstItem;
  133. Count++;
  134. }
  135. else
  136. {
  137. LastItem.Next = new Item(data); // ставим новый элемент после последнего и указываем на него из последнего
  138. LastItem = LastItem.Next; // делаем новый последним
  139. Count++;
  140. }
  141. }
  142. else
  143. {
  144. Console.WriteLine("Добавление элемента в коллекцию невозможно. Еще один элемент, и ваш ПК пойдет по швам.");
  145. }
  146. }
  147. public object Peek()
  148. {
  149. return FirstItem.Data;
  150. }
  151. public object[] ToArray()
  152. {
  153. if (Count == 0)
  154. {
  155. return null;
  156. }
  157. else
  158. {
  159. object[] array = new object[Count];
  160. Item currentItem = FirstItem;
  161. for (int i = 0; i < Count; i++)
  162. {
  163. Item cloneItem = currentItem.Clone() as Item;
  164. array[i] = cloneItem.Data;
  165. }
  166. return array;
  167. }
  168. }
  169. public object Clone()
  170. {
  171. Queue newQueue = new Queue(Capacity);
  172. if (Count != 0)
  173. {
  174. Item currentItem = FirstItem;
  175. newQueue.Enqueue(currentItem.Data);
  176.  
  177. for (int i = 0; i < Count-1; i++)
  178. {
  179. currentItem = currentItem.Next;
  180. newQueue.Enqueue(currentItem.Data);
  181. }
  182. }
  183. return newQueue;
  184. }
  185.  
  186. #endregion Methods
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement