Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DoubleLinkedList
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. DoubleLinkedList myList = new DoubleLinkedList();
  14.  
  15. myList.AddLast(2);
  16. myList.AddFirst(1);
  17. myList.AddFirst(0);
  18.  
  19. myList[2] = 3;
  20. myList.Remove(3);
  21. DoubleLinkedList myList2 = new DoubleLinkedList(myList);
  22. Console.WriteLine(myList[1]);
  23. Console.ReadKey();
  24. }
  25. }
  26. class DoubleLinkedList
  27. {
  28. ListElement first = null;
  29. ListElement last = null;
  30. int count = 0;
  31.  
  32. public int Count => count;
  33.  
  34. public DoubleLinkedList()
  35. {
  36. first = last = null;
  37. }
  38. public DoubleLinkedList(DoubleLinkedList list)
  39. {
  40. ListElement tempCopy = list.first;
  41. while (tempCopy != null)
  42. {
  43. this.AddLast(tempCopy.value);
  44. if (tempCopy.next != null)
  45. tempCopy = tempCopy.next;
  46. else
  47. tempCopy = null;
  48. }
  49. }
  50. public int this[int index]
  51. {
  52. get
  53. {
  54. if (index < 0 || index >= count) throw new IndexOutOfRangeException();
  55. ListElement temp = first;
  56. int i = index;
  57. while(i-- > 0)
  58. temp = temp.next;
  59. return temp.value;
  60. }
  61. set
  62. {
  63. if (index < 0 || index >= count) throw new IndexOutOfRangeException();
  64. ListElement temp = first;
  65. int i = index;
  66. while (i-- > 0)
  67. temp = temp.next;
  68. temp.value = value;
  69. }
  70. }
  71.  
  72. public void AddFirst(int value)
  73. {
  74. ListElement newElement = new ListElement(value);
  75. if (count == 0)
  76. {
  77. first = newElement;
  78. last = newElement;
  79. }
  80. else
  81. {
  82. newElement.next = first;
  83. first.prev = newElement;
  84. first = newElement;
  85. }
  86. count++;
  87. }
  88. public void AddLast(int value)
  89. {
  90. ListElement newElement = new ListElement(value);
  91. if (count == 0)
  92. {
  93. first = newElement;
  94. last = newElement;
  95. }
  96. else
  97. {
  98. newElement.prev = last;
  99. last.next = newElement;
  100. last = newElement;
  101. }
  102. count++;
  103. }
  104.  
  105. public void Remove(int value)
  106. {
  107. if (count <= 0) throw new Exception("List is empty!");
  108. ListElement temp = first;
  109. for(int i = 0; i < count; i++)
  110. {
  111. if(temp.value == value)
  112. {
  113. if (i == 0)
  114. first = first.next;
  115. if (i == count - 1)
  116. last = last.prev;
  117. if (temp.next != null)
  118. {
  119. temp.next.prev = temp.prev;
  120. temp.prev.next = temp.next;
  121. }
  122. else
  123. temp.prev.next = null;
  124. temp = null;
  125. count--;
  126. return;
  127. }
  128. temp = temp.next;
  129. }
  130. }
  131.  
  132. private class ListElement
  133. {
  134. public int value;
  135. public ListElement next = null;
  136. public ListElement prev = null;
  137. public ListElement(int Value, ListElement Next = null, ListElement Prev = null)
  138. {
  139. value = Value;
  140. next = Next;
  141. prev = Prev;
  142. }
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement