Updated Doubly linked list to allow for the combining of 2 lists

This commit is contained in:
Luke Else 2022-03-15 21:37:24 +00:00
parent 5c91fce224
commit 7ced980a90
2 changed files with 37 additions and 0 deletions

View File

@ -28,6 +28,30 @@ namespace C_.Datastructures
}; };
} }
public static DoublyLinkedList<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2)
{
//Create a new list from 2 separate lists
DoublyLinkedList<T> 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<T>? 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] public T? this[int i]
{ {
get get

View File

@ -16,6 +16,19 @@ list.Add(6);
list.Add(7); list.Add(7);
list.Add(8); list.Add(8);
DoublyLinkedList<int> list2 = new DoublyLinkedList<int>();
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<int>.Create(list, null);
var x = list[-6]; var x = list[-6];
Console.WriteLine(x); Console.WriteLine(x);