diff --git a/C#/Datastructures/DoublyLinkedList.cs b/C#/Datastructures/DoublyLinkedList.cs index ec2a5ad..54d8040 100644 --- a/C#/Datastructures/DoublyLinkedList.cs +++ b/C#/Datastructures/DoublyLinkedList.cs @@ -105,9 +105,12 @@ namespace C_.Datastructures } //Set tail to new item if index is the end - if (index == Count) + if (index == Count - 1) { + //Decrement count as it will be be re-incremented once appended + Count--; Append(value); + return; } //Fetch point in list and add new item diff --git a/C#/Program.cs b/C#/Program.cs index ace465f..3dc4c16 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -29,14 +29,15 @@ list.Append(3); list.Append(4); list.Append(5); list.Append(6); +list.Insert(6, 1); Console.Write($"{list[0]} "); Console.Write($"{list[1]} "); Console.Write($"{list[2]} "); Console.Write($"{list[3]} "); Console.Write($"{list[4]} "); -Console.Write($"{list[5]} \n"); -Console.Write($"{list[6]} "); +Console.Write($"{list[5]} "); +Console.Write($"{list[6]} \n"); //DoublyLinkedList list2 = new DoublyLinkedList();