Bugs fixed on Delete and Insert Methods

This commit is contained in:
Luke Else 2022-03-19 00:00:13 +00:00
parent f2c3e74d15
commit e6557a1d90
3 changed files with 58 additions and 40 deletions

View File

@ -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<T>? node = Traverse(index - 1);
node!.Next = DoublyLinkedListNode<T>.Create(value, node.Next, node);
@ -113,24 +119,39 @@ namespace C_.Datastructures
node.Next.Next.Prev = node.Next;
}
public bool Delete(int index){
DoublyLinkedListNode<T>? 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<T>? 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;
}

View File

@ -62,8 +62,8 @@ namespace C_.Datastructures
public void Append(T value){
//Create new node
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
Count++;
LinkedListNode<T> newItem = LinkedListNode<T>.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;

View File

@ -5,7 +5,8 @@ using C_.Datastructures.Nodes;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
//DoublyLinkedList<int> list = new DoublyLinkedList<int>();
//LinkedList<int> list = new LinkedList<int>();
//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<int> list = new DoublyLinkedList<int>();
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<int> list2 = new DoublyLinkedList<int>();
@ -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<int> list = new LinkedList<int>();
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();