Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Iterator
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //Numbers numbers = new Numbers();
  15. //foreach (int n in numbers)
  16. //{
  17. // Console.WriteLine(n);
  18. //}
  19.  
  20. DoublyLinkedList<object> list = new DoublyLinkedList<object>();
  21. list.AddFirst("Harry");
  22. list.Add("Bob");
  23. list.Add("John");
  24. list.Add("Kate");
  25. list.Add(3.1416);
  26.  
  27. list.Remove("John");
  28.  
  29. foreach (var x in list.BackEnumerator())
  30. {
  31. Console.WriteLine(x);
  32. }
  33. }
  34. }
  35.  
  36. class DoublyNode<T>
  37. {
  38. public T Data { get; set; }
  39. public DoublyNode<T> Previous { get; set; }
  40. public DoublyNode<T> Next { get; set; }
  41.  
  42. public DoublyNode(T data)
  43. {
  44. Data = data;
  45. }
  46. }
  47.  
  48. class DoublyLinkedList<T> : IEnumerable<T>
  49. {
  50. DoublyNode<T> head;
  51. DoublyNode<T> tail;
  52. int count;
  53.  
  54. public void Add(T data)
  55. {
  56. DoublyNode<T> node = new DoublyNode<T>(data);
  57. if (head == null)
  58. {
  59. head = node;
  60. }
  61. else
  62. {
  63. node.Previous = tail;
  64. tail.Next = node;
  65. }
  66. tail = node;
  67. count++;
  68. }
  69.  
  70. public void AddFirst(T data)
  71. {
  72. DoublyNode<T> node = new DoublyNode<T>(data);
  73. if (tail == null)
  74. {
  75. tail = node;
  76. }
  77. else
  78. {
  79. node.Next = head;
  80. head.Previous = node;
  81. }
  82. head = node;
  83. count++;
  84. }
  85.  
  86. public bool Remove(T data)
  87. {
  88. bool flag = false;
  89.  
  90. DoublyNode<T> current = head;
  91.  
  92. while (current != null)
  93. {
  94. if (current.Data.Equals(data))
  95. {
  96. break;
  97. }
  98. current = current.Next;
  99. }
  100.  
  101. if (current != null)
  102. {
  103. current.Next.Previous = current.Previous;
  104. current.Previous.Next = current.Next;
  105. }
  106.  
  107. return flag;
  108. }
  109.  
  110. public IEnumerable<T> BackEnumerator()
  111. {
  112. DoublyNode<T> current = tail;
  113. while (current != null)
  114. {
  115. yield return current.Data;
  116. current = current.Previous;
  117. }
  118. }
  119.  
  120. public IEnumerator<T> GetEnumerator()
  121. {
  122. DoublyNode<T> current = head;
  123. while (current != null)
  124. {
  125. yield return current.Data;
  126. current = current.Next;
  127. }
  128. }
  129.  
  130. IEnumerator IEnumerable.GetEnumerator()
  131. {
  132. return GetEnumerator();
  133. }
  134. }
  135.  
  136. class Numbers
  137. {
  138. public IEnumerator GetEnumerator()
  139. {
  140. for (int i = 0; i < 6; i++)
  141. {
  142. if (i == 3)
  143. {
  144. yield break;
  145. }
  146. yield return i * i;
  147. }
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement