diff --git a/C#/Datastructures/DoublyLinkedList.cs b/C#/Datastructures/DoublyLinkedList.cs index fa9caf6..ec2a5ad 100644 --- a/C#/Datastructures/DoublyLinkedList.cs +++ b/C#/Datastructures/DoublyLinkedList.cs @@ -93,8 +93,8 @@ namespace C_.Datastructures public void Insert(int index, T value) { - if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); Count++; + if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds"); //Set head to new item if list is empty if (index == 0 || Head == null) @@ -104,6 +104,12 @@ namespace C_.Datastructures return; } + //Set tail to new item if index is the end + if (index == Count) + { + Append(value); + } + //Fetch point in list and add new item DoublyLinkedListNode? node = Traverse(index - 1); node!.Next = DoublyLinkedListNode.Create(value, node.Next, node); @@ -113,24 +119,39 @@ namespace C_.Datastructures node.Next.Next.Prev = node.Next; } - public bool Delete(int index){ - DoublyLinkedListNode? node = Traverse(index); + public void Delete(int index) + { + Count--; + if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds"); - if (node == default) return false; - - if(node.Prev != default){ - //connect item beofre to item after node we are deleting - node.Prev.Next = node.Next; + //Check if we are trying to reference the first item + if (index == 0 && Head != default) + { + Head = Head!.Next; + if(Head != default) + Head.Prev = default; + return; } + //Set tail to new item if index is the end + if (index == Count && Tail != default) + { + Tail = Tail!.Prev; + + if(Tail != default) + Tail.Next = default; + return; + } + + DoublyLinkedListNode? node = Traverse(index - 1); + + node!.Next = node.Next!.Next; + //Connect item after to the the item before the node we are deleting if (node.Next != default) { - node.Next.Prev = node.Prev; + node.Next.Prev = node; } - - Count--; - return true; } diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs index 5ad7848..d2b1b0a 100644 --- a/C#/Datastructures/LinkedList.cs +++ b/C#/Datastructures/LinkedList.cs @@ -62,8 +62,8 @@ namespace C_.Datastructures public void Append(T value){ //Create new node - LinkedListNode newItem = LinkedListNode.Create(value, default); Count++; + LinkedListNode newItem = LinkedListNode.Create(value, default); //Set head to new item if list is empty if (Head == null) @@ -81,14 +81,12 @@ namespace C_.Datastructures //Append item to end end!.Next = newItem; - } public void Insert(int index, T value) { - if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); - Count++; + if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds"); //Set head to new item if list is empty if (index == 0 || Head == null) @@ -105,12 +103,11 @@ namespace C_.Datastructures public void Delete(int index) { - if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); - Count--; + if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds"); //Check if we are trying to reference the first item - if (index == 0 && Head != null) + if (index == 0 && Head != default) { Head = Head!.Next; return; diff --git a/C#/Program.cs b/C#/Program.cs index 2c2e3aa..18a8468 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -5,7 +5,8 @@ using C_.Datastructures.Nodes; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); -//DoublyLinkedList list = new DoublyLinkedList(); + +//LinkedList list = new LinkedList(); //list.Append(1); //list.Append(2); @@ -13,8 +14,25 @@ Console.WriteLine("Hello, World!"); //list.Append(4); //list.Append(5); //list.Append(6); -//list.Append(7); -//list.Append(8); + +//list.Delete(5); +//list.Delete(2); +//list.Delete(0); + + + +DoublyLinkedList list = new DoublyLinkedList(); + +list.Append(1); +list.Append(2); +list.Append(3); +list.Append(4); +list.Append(5); +list.Append(6); + +list.Delete(5); +list.Delete(3); +list.Delete(0); //DoublyLinkedList list2 = new DoublyLinkedList(); @@ -27,27 +45,9 @@ Console.WriteLine("Hello, World!"); //list2.Append(7); //list2.Append(8); -//list.Insert(9, 5); - -//list.Delete(2); - //var x = list[-6]; -//Console.WriteLine(x); -LinkedList list = new LinkedList(); - -list.Append(1); -list.Append(2); -list.Append(3); -list.Append(4); -list.Append(5); -list.Append(6); - -list.Insert(0, 200); -list.Insert(2, 100); -list.Delete(2); -list.Delete(0); Console.ReadLine();