diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs index c9c21ba..5ad7848 100644 --- a/C#/Datastructures/LinkedList.cs +++ b/C#/Datastructures/LinkedList.cs @@ -61,6 +61,7 @@ namespace C_.Datastructures } public void Append(T value){ + //Create new node LinkedListNode newItem = LinkedListNode.Create(value, default); Count++; @@ -77,44 +78,48 @@ namespace C_.Datastructures { end = Traverse(); } - + //Append item to end - end!.Next = new LinkedListNode{ - Value = value, - Next = default - }; + end!.Next = newItem; + } public void Insert(int index, T value) { if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); + Count++; + + //Set head to new item if list is empty + if (index == 0 || Head == null) + { + Head = LinkedListNode.Create(value, Head); + return; + } + //Fetch point in list at which item will be added LinkedListNode? node = Traverse(index - 1); - node!.Next = new LinkedListNode { - Value = value, - Next = node.Next - }; - Count++; + node!.Next = LinkedListNode.Create(value, node!.Next); } public void Delete(int index) { + if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); + + Count--; + //Check if we are trying to reference the first item if (index == 0 && Head != null) { - Head = (LinkedListNode?)Head!.Next; + Head = Head!.Next; return; } - //Find node we are trying to delete and then remove / relink + //Find node before the one we are trying to delete and then remove / relink LinkedListNode? node = Traverse(index - 1); - if (node == null || node.Next == null) throw new System.Exception("Error! Index outside of Bounds"); - - node.Next = node.Next.Next; - Count--; + node!.Next = node.Next!.Next; } private LinkedListNode? Traverse(){ diff --git a/C#/Program.cs b/C#/Program.cs index 61bc7ed..2c2e3aa 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -5,7 +5,37 @@ using C_.Datastructures.Nodes; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); -DoublyLinkedList list = new DoublyLinkedList(); +//DoublyLinkedList list = new DoublyLinkedList(); + +//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.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); + +//var x = list[-6]; + +//Console.WriteLine(x); + +LinkedList list = new LinkedList(); list.Append(1); list.Append(2); @@ -13,25 +43,13 @@ list.Append(3); list.Append(4); list.Append(5); list.Append(6); -list.Append(7); -list.Append(8); - -DoublyLinkedList list2 = new DoublyLinkedList(); - -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.Insert(0, 200); +list.Insert(2, 100); list.Delete(2); +list.Delete(0); + +Console.ReadLine(); -var x = list[-6]; -Console.WriteLine(x);