Advertisement
Montoya-Romina-Anahi

Tp4_Punto5_List

Jul 7th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. using System;
  2.  
  3. using System.Collections;
  4.  
  5. using System.Collections.Generic;
  6.  
  7. using System.Linq;
  8.  
  9. using System.Text;
  10.  
  11. using System.Threading.Tasks;
  12.  
  13.  
  14.  
  15. namespace ConsoleApp5
  16. {
  17. class List<E> : IEnumerable where E : IComparable
  18. {
  19. private Node head;
  20. private int count;
  21. private Node tail;
  22. public int getCount()
  23. {
  24. return this.count;
  25. }
  26. public List()
  27. {
  28. this.head = null;
  29. this.count = 0;
  30. this.tail = null;
  31. }
  32. public void AddFirst(E item)
  33. {
  34. if (this.count == 0)
  35. {
  36. this.head = this.tail = new Node(item, null, null);
  37. ++this.count;
  38. }
  39. else
  40. {
  41. Node temp = new Node(item, null, null);
  42. temp.next = this.head;
  43. this.head.prev = temp;
  44. this.head = temp;
  45. ++this.count;
  46. }
  47. }
  48.  
  49. public void BetterAddFirst(E item)
  50. {
  51. Node temp = new Node(item, this.head, null);
  52. if (this.count == 0)
  53. {
  54. this.tail = temp;
  55. }
  56. else
  57. {
  58. this.head.prev = temp;
  59. }
  60. this.head = temp;
  61. ++this.count;
  62. }
  63. public void AddLast(E item)
  64. {
  65. if (this.count == 0)
  66. {
  67. this.head = this.tail = new Node(item, null, null);
  68. ++this.count;
  69. }
  70. else
  71. {
  72. Node temp = new Node(item, null, null);
  73. temp.prev = this.tail;
  74. this.tail.next = temp;
  75. this.tail = temp;
  76. ++this.count;
  77.  
  78. }
  79.  
  80. }
  81.  
  82. public void BetterAddLast(E item)
  83. {
  84. Node temp = new Node(item, null, this.tail);
  85. if (this.count == 0)
  86. {
  87. this.head = temp;
  88. }
  89. else
  90. {
  91. this.tail.next = temp;
  92. }
  93. this.tail = temp;
  94. ++this.count;
  95. }
  96. public void AddInOrder(E item)
  97. {
  98. if (this.count == 0)
  99. {
  100. this.head = this.tail = new Node(item, null, null);
  101. ++this.count;
  102. }
  103. else
  104. {
  105. if (item.CompareTo(this.head.item) <= 0)
  106. {
  107. this.AddFirst(item);
  108. }
  109. else
  110. {
  111. if (item.CompareTo(this.tail.item) > 0)
  112. {
  113. this.AddLast(item);
  114. }
  115. else
  116. {
  117. Node skip = this.head;
  118. for (; (skip != null) && (item.CompareTo(skip.item) > 0); skip = skip.next) { }
  119. if (skip == null)
  120. {
  121. throw new Exception("fallo en el orden de los elementos");
  122. }
  123. else
  124. {
  125. Node temp = new Node(item, skip, skip.prev);
  126. skip.prev.next = temp;
  127. skip.prev = temp;
  128. ++this.count;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. public E RemoveFirst()
  135. {
  136. if (this.count == 0)
  137. {
  138. throw new Exception("LA LISTA ESTA VACIA!");
  139. }
  140. E item = this.head.item;
  141. this.head = this.head.next;
  142. if (this.head == null)
  143. {
  144. this.tail = null;
  145. }
  146. else
  147. {
  148. this.head.prev = null;
  149. }
  150. --this.count;
  151. return item;
  152. }
  153. public E RemoveLast()
  154. {
  155. if (this.count == 0)
  156. {
  157. throw new Exception("LA LISTA ESTA VACIA");
  158. }
  159. E item = this.tail.item;
  160. if (this.head.next == null)
  161. {
  162. this.head = this.tail = null;
  163. }
  164. else
  165. {
  166. this.tail = this.tail.prev;
  167. this.tail.next = null;
  168. }
  169. --this.count;
  170. return item;
  171. }
  172. public bool FindAndRemove(E item)
  173. {
  174. if (this.count == 0)
  175. {
  176. return false;
  177. }
  178. Node skip = this.head;
  179. for (; (skip != null) && !(item.CompareTo(skip.item) == 0); skip = skip.next) { }
  180. if (skip == null)
  181. {
  182. return false;
  183. }
  184. else
  185. {
  186. if (skip.prev == null)
  187. {
  188. this.RemoveFirst();
  189. return true;
  190. }
  191. else
  192. {
  193. if (skip.next == null)
  194. {
  195. this.RemoveLast();
  196. return true;
  197. }
  198. else
  199. {
  200. skip.prev.next = skip.next;
  201. skip.next.prev = skip.prev;
  202. skip.prev = skip.next = null;
  203. return true;
  204. }
  205. }
  206. }
  207. }
  208. private class Node
  209. {
  210. public E item;
  211. public Node next;
  212. public Node prev;
  213. public Node()
  214. {
  215. this.item = default(E);
  216. this.next = null;
  217. this.prev = null;
  218. }
  219. public Node(E item)
  220. {
  221. this.item = item;
  222. this.next = null;
  223. this.prev = null;
  224. }
  225. public Node(E item, Node next)
  226. {
  227. this.item = item;
  228. this.next = next;
  229. this.prev = null;
  230. }
  231. public Node(E item, Node next, Node prev)
  232. {
  233. this.item = item;
  234. this.next = next;
  235. this.prev = prev;
  236. }
  237. public override string ToString()
  238. {
  239. return this.item.ToString();
  240. }
  241. }
  242. public IEnumerator GetEnumerator()
  243. {
  244. return MyEnumerator();
  245. }
  246. private IEnumerator MyEnumerator()
  247. {
  248. for (Node skip = head; skip != null; skip = skip.next)
  249. {
  250. yield return skip.item;
  251.  
  252. }
  253. }
  254. }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement