Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApp1
  6. {
  7. class MyLinkedList<T> : IList<T> where T : class
  8. {
  9. class LinkedListItem
  10. {
  11. public T Value { get; set; }
  12. public LinkedListItem Next { get; set; }
  13.  
  14. public override string ToString()
  15. {
  16. return new { Value, Next }.ToString();
  17. }
  18. }
  19.  
  20. class LinkedListEnumerator : IEnumerator<T>
  21. {
  22. readonly MyLinkedList<T> _parent;
  23. LinkedListItem _currentItem;
  24. public LinkedListEnumerator(MyLinkedList<T> parent)
  25. {
  26. _parent = parent;
  27. }
  28.  
  29. public T Current { get { return _currentItem?.Value; } }
  30. object IEnumerator.Current { get { return Current; } }
  31.  
  32. public void Dispose()
  33. {
  34. }
  35.  
  36. public bool MoveNext()
  37. {
  38. if (_currentItem == null)
  39. {
  40. Reset();
  41. return true;
  42. }
  43. _currentItem = _currentItem?.Next;
  44. return _currentItem != null;
  45. }
  46.  
  47. public void Reset()
  48. {
  49. _currentItem = _parent._head;
  50. }
  51. }
  52.  
  53. LinkedListItem _head;
  54.  
  55. public int Count { get; private set; }
  56. public bool IsReadOnly { get; } = false;
  57.  
  58. public T this[int index]
  59. {
  60. get
  61. {
  62. if (index < 0 || index >= Count)
  63. throw new IndexOutOfRangeException();
  64. var i = 0;
  65. var current = _head;
  66. while (i++ != index)
  67. current = current.Next;
  68. return current.Value;
  69. }
  70. set
  71. {
  72. if (index < 0 || index >= Count)
  73. throw new IndexOutOfRangeException();
  74. var i = 0;
  75. var current = _head;
  76. while (i++ != index)
  77. current = current.Next;
  78. current.Value = value;
  79. }
  80. }
  81. public void Add(T item)
  82. {
  83. if (_head == null)
  84. _head = new LinkedListItem { Value = item };
  85. else
  86. {
  87. var current = _head;
  88. while (current.Next != null)
  89. current = current.Next;
  90. current.Next = new LinkedListItem { Value = item };
  91. }
  92. Count++;
  93. }
  94.  
  95. public bool Remove(T value)
  96. {
  97. var current = _head;
  98. var prev = default(LinkedListItem);
  99. while (current != null && current.Value != value)
  100. {
  101. prev = current;
  102. current = current.Next;
  103. }
  104. if (current == null)
  105. return false;
  106. if (prev == null)
  107. _head = current.Next;
  108. else
  109. prev.Next = current.Next;
  110. Count--;
  111. return true;
  112. }
  113.  
  114. public int IndexOf(T item)
  115. {
  116. var current = _head;
  117. var index = 0;
  118. while (current != null && current.Value != item)
  119. {
  120. index++;
  121. current = current.Next;
  122. }
  123. return current == null ? -1 : index;
  124. }
  125.  
  126. public void Insert(int index, T item)
  127. {
  128. if (index < 0 || index >= Count)
  129. throw new ArgumentException(nameof(index));
  130. var current = _head;
  131. var prev = default(LinkedListItem);
  132. var i = 0;
  133. while(i++ < index)
  134. {
  135. prev = current;
  136. current = current.Next;
  137. }
  138. if (prev != null)
  139. prev.Next = new LinkedListItem { Value = item, Next = current };
  140. else
  141. _head = new LinkedListItem { Value = item, Next = current };
  142. Count++;
  143. }
  144.  
  145. public void RemoveAt(int index)
  146. {
  147. if (index < 0 || index >= Count)
  148. throw new ArgumentException(nameof(index));
  149. var current = _head;
  150. var prev = default(LinkedListItem);
  151. var i = 0;
  152. while (i++ < index)
  153. {
  154. prev = current;
  155. current = current.Next;
  156. }
  157. if (prev != null)
  158. prev.Next = current.Next;
  159. else
  160. _head = current.Next;
  161. Count--;
  162. }
  163.  
  164. public void Clear()
  165. {
  166. _head = null;
  167. Count = 0;
  168. }
  169.  
  170. public bool Contains(T item)
  171. {
  172. return IndexOf(item) > -1;
  173. }
  174.  
  175. public void CopyTo(T[] array, int arrayIndex)
  176. {
  177. if (array.Length < arrayIndex + Count)
  178. throw new ArgumentException(nameof(array));
  179. var current = _head;
  180. for (var i = arrayIndex; i < arrayIndex + Count; i++)
  181. {
  182. array[i] = current.Value;
  183. current = current.Next;
  184. }
  185. }
  186.  
  187. public IEnumerator<T> GetEnumerator()
  188. {
  189. return new LinkedListEnumerator(this);
  190. }
  191.  
  192. IEnumerator IEnumerable.GetEnumerator()
  193. {
  194. return GetEnumerator();
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement