From 2828466e23e0fb443ce20805e4c1b963261092e7 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 16 Mar 2022 22:31:54 +0000 Subject: [PATCH] Insert Method added to Doubly Linked list --- C#/Datastructures/DoublyLinkedList.cs | 38 ++++++++++++++++++++------- C#/Datastructures/LinkedList.cs | 1 + C#/Program.cs | 34 +++++++++++++----------- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/C#/Datastructures/DoublyLinkedList.cs b/C#/Datastructures/DoublyLinkedList.cs index 1c1bcfa..b93eae9 100644 --- a/C#/Datastructures/DoublyLinkedList.cs +++ b/C#/Datastructures/DoublyLinkedList.cs @@ -23,7 +23,7 @@ namespace C_.Datastructures //Create a new Class with a single item return new DoublyLinkedList() { - Head = DoublyLinkedListNode.Create(value, null, null), + Head = DoublyLinkedListNode.Create(value, default, default), Count = 1 }; } @@ -36,10 +36,10 @@ namespace C_.Datastructures DoublyLinkedList list; list = list1; - if (list == null || list.Count == 0) return list2; + if (list == default || list.Count == 0) return list2; //Find end of list and append fist item of next list - if (list2 == null || list.Count == 0) return list; + if (list2 == default || list.Count == 0) return list; DoublyLinkedListNode? end = list.Traverse(); @@ -61,7 +61,7 @@ namespace C_.Datastructures //Return Value DoublyLinkedListNode? node = Traverse(i); - if (node != null) return node.Value; + if (node != default) return node.Value; return default; } set @@ -74,22 +74,42 @@ namespace C_.Datastructures node!.Value = value; } } - public void Add(T value) + public void Append(T value) { Count++; if (Tail != null && Count > 0) { - Tail.Next = new DoublyLinkedListNode { Value = value, Next = null, Prev = Tail }; + Tail.Next = new DoublyLinkedListNode { Value = value, Next = default, Prev = Tail }; Tail = Tail.Next; return; } - Head = new DoublyLinkedListNode { Value = value, Next = null, Prev = null }; + Head = new DoublyLinkedListNode { Value = value, Next = default, Prev = default }; Tail = Head; } + public void Insert(int index, T value) + { + if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); + + //Fetch point in list and add new item + DoublyLinkedListNode? node = Traverse(index - 1); + node!.Next = new DoublyLinkedListNode{ + Value = value, + Next = node.Next, + Prev = node + }; + + //Create backlink in the list + if(node.Next.Next != default) + node.Next.Next.Prev = node.Next; + + Count++; + + } + public bool Delete(int index){ DoublyLinkedListNode? node = Traverse(index); @@ -153,7 +173,7 @@ namespace C_.Datastructures } private DoublyLinkedListNode? Next(DoublyLinkedListNode current) { - if (current != null) + if (current != default) return current.Next; return null; @@ -161,7 +181,7 @@ namespace C_.Datastructures private DoublyLinkedListNode? Prev(DoublyLinkedListNode current) { - if (current != null) + if (current != default) return current.Prev; return null; diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs index 36869ca..c9c21ba 100644 --- a/C#/Datastructures/LinkedList.cs +++ b/C#/Datastructures/LinkedList.cs @@ -91,6 +91,7 @@ namespace C_.Datastructures //Fetch point in list at which item will be added LinkedListNode? node = Traverse(index - 1); + node!.Next = new LinkedListNode { Value = value, Next = node.Next diff --git a/C#/Program.cs b/C#/Program.cs index d75807d..61bc7ed 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -7,25 +7,27 @@ Console.WriteLine("Hello, World!"); DoublyLinkedList list = new DoublyLinkedList(); -list.Add(1); -list.Add(2); -list.Add(3); -list.Add(4); -list.Add(5); -list.Add(6); -list.Add(7); -list.Add(8); +list.Append(1); +list.Append(2); +list.Append(3); +list.Append(4); +list.Append(5); +list.Append(6); +list.Append(7); +list.Append(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); +list2.Append(1); +list2.Append(2); +list2.Append(3); +list2.Append(4); +list2.Append(5); +list2.Append(6); +list2.Append(7); +list2.Append(8); + +list.Insert(9, 5); list.Delete(2);