nassss

Untitled

Mar 27th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace DictionaryWorkshop.Implementation
  7. {
  8. public class MyLinkedList<TKey, TValue> : IEnumerable<MyKeyValuePair<TKey,TValue>>
  9. {
  10. public MyKeyValuePair<TKey, TValue> Head { get; private set; }
  11. public MyKeyValuePair<TKey, TValue> Tail { get; private set; }
  12.  
  13. public MyLinkedList(TKey key, TValue value)
  14. {
  15. this.Head = new MyKeyValuePair<TKey, TValue>(key, value);
  16. this.Tail = this.Head;
  17. this.Count++;
  18. }
  19.  
  20. public int Count { get; set; }
  21.  
  22. public bool Add(TKey key, TValue value)
  23. {
  24. var oldTail = this.Tail;
  25. var newNode = new MyKeyValuePair<TKey, TValue>(key, value);
  26.  
  27. if (this.Count == 0 && this.Contains(key)==false)
  28. {
  29. this.Tail = newNode;
  30. this.Head = this.Tail;
  31. this.Count++;
  32. return true;
  33. }
  34. else if (this.Contains(key) == false)
  35. {
  36. oldTail.Next = newNode;
  37. this.Tail = newNode;
  38. this.Count++;
  39. return true;
  40. }
  41. return false;
  42. }
  43. public bool Remove(TKey key)
  44. {
  45. MyKeyValuePair<TKey, TValue> current = this.Head;
  46. if (this.Count == 0)
  47. {
  48. throw new InvalidOperationException();
  49. }
  50. if (this.Contains(key)==false )
  51. {
  52. return false;
  53. }
  54. if (this.Head.Key.Equals(key) && this.Tail.Key.Equals(key))
  55. {
  56. this.Head = null;
  57. this.Tail = null;
  58. this.Count--;
  59. return true;
  60. }
  61. else if (this.Tail.Key.Equals(key))
  62. {
  63. var currentNode = this.Head;
  64.  
  65. while (currentNode !=null)
  66. {
  67. if (currentNode.Next == this.Tail)
  68. {
  69. currentNode.Next = null;
  70. currentNode = this.Tail;
  71. this.Count--;
  72. return true;
  73. }
  74. currentNode = currentNode.Next;
  75. }
  76. }
  77. while (current !=null)
  78. {
  79. if (current.Next.Key.Equals(key))
  80. {
  81. var previous = current;
  82. previous.Next = previous.Next.Next;
  83. this.Count--;
  84. return true;
  85. }
  86. current = current.Next;
  87. }
  88. return false;
  89. }
  90. public bool Contains(TKey key)
  91. {
  92. MyKeyValuePair<TKey, TValue> current = this.Head;
  93.  
  94. while (current !=null)
  95. {
  96. if (current.Key.Equals(key))
  97. {
  98. return true;
  99. }
  100. current = current.Next;
  101. }
  102. return false;
  103. }
  104.  
  105. public IEnumerator<MyKeyValuePair<TKey, TValue>> GetEnumerator()
  106. {
  107. MyKeyValuePair<TKey, TValue> current = this.Head;
  108.  
  109. while (current != null)
  110. {
  111. yield return current;
  112. current = current.Next;
  113. }
  114. }
  115.  
  116. IEnumerator IEnumerable.GetEnumerator()
  117. {
  118. return GetEnumerator();
  119. }
  120. public override string ToString()
  121. {
  122. var sb = new StringBuilder();
  123. var temp = this.Head;
  124. while (temp !=null)
  125. {
  126. sb.AppendLine(temp.ToString());
  127. temp = temp.Next;
  128. }
  129. return sb.ToString();
  130. }
  131. }
  132. }
Add Comment
Please, Sign In to add comment