From 7ced980a904d9c0488aa6fe7b0d8c483221213a6 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 15 Mar 2022 21:37:24 +0000 Subject: [PATCH] Updated Doubly linked list to allow for the combining of 2 lists --- C#/Datastructures/DoublyLinkedList.cs | 24 ++++++++++++++++++++++++ C#/Program.cs | 13 +++++++++++++ 2 files changed, 37 insertions(+) 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);