Bugs fixed on Delete and Insert Methods
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user