Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. namespace Workshop
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Text.RegularExpressions;
  7.  
  8. public class DoublyLinkedList<T>
  9. where T : IComparable<T>
  10. {
  11. public class LinkNode
  12. {
  13. public LinkNode(T value)
  14. {
  15. this.Value = value;
  16. }
  17.  
  18. public T Value { get; }
  19.  
  20. public LinkNode NextNode { get; set; }
  21.  
  22. public LinkNode PreviousNode { get; set; }
  23. }
  24.  
  25. private LinkNode head;
  26. private LinkNode tail;
  27.  
  28. public int Count { get; private set; }
  29.  
  30. public void AddFirst(T value)
  31. {
  32. LinkNode newHead = new LinkNode(value);
  33.  
  34. if (this.Count == 0)
  35. {
  36. this.tail = this.head = newHead;
  37. }
  38. else
  39. {
  40. newHead.NextNode = this.head;
  41. this.head.PreviousNode = newHead;
  42. this.head = newHead;
  43. }
  44.  
  45. this.Count++;
  46. }
  47.  
  48. public void AddLast(T value)
  49. {
  50. LinkNode newTail = new LinkNode(value);
  51.  
  52. if (this.Count == 0)
  53. {
  54. this.tail = this.head = newTail;
  55. }
  56. else
  57. {
  58. newTail.PreviousNode = this.tail;
  59. this.tail.NextNode = newTail;
  60. this.tail = newTail;
  61. }
  62.  
  63. this.Count++;
  64. }
  65.  
  66. public T RemoveFirst()
  67. {
  68. CheckIfEmptyThrowException();
  69.  
  70. T firstElement = this.head.Value;
  71. this.head = this.head.NextNode;
  72.  
  73. if (this.head == null)
  74. {
  75. this.tail = null;
  76. }
  77. else
  78. {
  79. this.head.PreviousNode = null;
  80. }
  81.  
  82. this.Count--;
  83.  
  84. return firstElement;
  85. }
  86.  
  87. public T RemoveLast()
  88. {
  89. CheckIfEmptyThrowException();
  90.  
  91. T lastElement = this.tail.Value;
  92. this.tail = this.tail.PreviousNode;
  93.  
  94. if (this.tail == null)
  95. {
  96. this.head = null;
  97. }
  98. else
  99. {
  100. this.tail.NextNode = null;
  101. }
  102.  
  103. this.Count--;
  104.  
  105. return lastElement;
  106. }
  107.  
  108. public void Print(Action<T> action)
  109. {
  110. LinkNode currentNode = this.head;
  111.  
  112. while (currentNode != null)
  113. {
  114. action(currentNode.Value);
  115.  
  116. currentNode = currentNode.NextNode;
  117. }
  118. }
  119.  
  120. public bool Contains(T value)
  121. {
  122. LinkNode currentNode = this.head;
  123.  
  124. while (currentNode != null)
  125. {
  126. if (currentNode.Value.CompareTo(value) == 0)
  127. {
  128. return true;
  129. }
  130.  
  131. currentNode = currentNode.NextNode;
  132. }
  133.  
  134. return false;
  135. }
  136.  
  137. public T[] ToArray()
  138. {
  139. T[] array = new T[this.Count];
  140.  
  141. var currentNode = this.head;
  142. int counter = 0;
  143.  
  144. while (currentNode != null)
  145. {
  146. array[counter++] = currentNode.Value;
  147.  
  148. currentNode = currentNode.NextNode;
  149. }
  150.  
  151. return array;
  152. }
  153.  
  154. public List<T> ToList()
  155. {
  156. List<T> list = new List<T>(this.Count);
  157.  
  158. var currentNode = this.head;
  159.  
  160. while (currentNode != null)
  161. {
  162. list.Add(currentNode.Value);
  163.  
  164. currentNode = currentNode.NextNode;
  165. }
  166.  
  167. return list;
  168. }
  169.  
  170. private void CheckIfEmptyThrowException()
  171. {
  172. if (this.Count == 0)
  173. {
  174. throw new InvalidOperationException("DoublyLinkedList is empty!");
  175. }
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement