diff --git a/C#/Datastructures/DoublyLinkedList.cs b/C#/Datastructures/DoublyLinkedList.cs index ea3a220..de3d594 100644 --- a/C#/Datastructures/DoublyLinkedList.cs +++ b/C#/Datastructures/DoublyLinkedList.cs @@ -28,6 +28,30 @@ namespace C_.Datastructures }; } + + public static DoublyLinkedList Create(DoublyLinkedList list1, DoublyLinkedList list2) + { + //Create a new list from 2 separate lists + + DoublyLinkedList list; + + list = list1; + if (list == null || list.Count == 0) return list2; + + //Find end of list and append fist item of next list + if (list2 == null || list.Count == 0) return list; + + DoublyLinkedListNode? end = list.Traverse(); + + //Connect up pointers at ajoining section + end!.Next = list2!.Head; + end!.Next!.Prev = end; + end = list2.Tail; + list.Count += list2!.Count; + + return list; + } + public T? this[int i] { get diff --git a/C#/Program.cs b/C#/Program.cs index 57d8941..09b662d 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -16,6 +16,19 @@ list.Add(6); list.Add(7); list.Add(8); +DoublyLinkedList list2 = new DoublyLinkedList(); + +list2.Add(1); +list2.Add(2); +list2.Add(3); +list2.Add(4); +list2.Add(5); +list2.Add(6); +list2.Add(7); +list2.Add(8); + +var list3 = DoublyLinkedList.Create(list, null); + var x = list[-6]; Console.WriteLine(x);