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) public void Insert(int index, T value)
{ {
if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
Count++; Count++;
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
//Set head to new item if list is empty //Set head to new item if list is empty
if (index == 0 || Head == null) if (index == 0 || Head == null)
@ -104,6 +104,12 @@ namespace C_.Datastructures
return; return;
} }
//Set tail to new item if index is the end
if (index == Count)
{
Append(value);
}
//Fetch point in list and add new item //Fetch point in list and add new item
DoublyLinkedListNode<T>? node = Traverse(index - 1); DoublyLinkedListNode<T>? node = Traverse(index - 1);
node!.Next = DoublyLinkedListNode<T>.Create(value, node.Next, node); node!.Next = DoublyLinkedListNode<T>.Create(value, node.Next, node);
@ -113,24 +119,39 @@ namespace C_.Datastructures
node.Next.Next.Prev = node.Next; node.Next.Next.Prev = node.Next;
} }
public bool Delete(int index){ public void Delete(int index)
DoublyLinkedListNode<T>? node = Traverse(index); {
Count--;
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
if (node == default) return false; //Check if we are trying to reference the first item
if (index == 0 && Head != default)
if(node.Prev != default){ {
//connect item beofre to item after node we are deleting Head = Head!.Next;
node.Prev.Next = node.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 //Connect item after to the the item before the node we are deleting
if (node.Next != default) 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){ public void Append(T value){
//Create new node //Create new node
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
Count++; Count++;
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
//Set head to new item if list is empty //Set head to new item if list is empty
if (Head == null) if (Head == null)
@ -81,14 +81,12 @@ namespace C_.Datastructures
//Append item to end //Append item to end
end!.Next = newItem; end!.Next = newItem;
} }
public void Insert(int index, T value) public void Insert(int index, T value)
{ {
if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
Count++; Count++;
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
//Set head to new item if list is empty //Set head to new item if list is empty
if (index == 0 || Head == null) if (index == 0 || Head == null)
@ -105,12 +103,11 @@ namespace C_.Datastructures
public void Delete(int index) public void Delete(int index)
{ {
if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
Count--; 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 //Check if we are trying to reference the first item
if (index == 0 && Head != null) if (index == 0 && Head != default)
{ {
Head = Head!.Next; Head = Head!.Next;
return; return;

View File

@ -5,7 +5,8 @@ using C_.Datastructures.Nodes;
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
//DoublyLinkedList<int> list = new DoublyLinkedList<int>();
//LinkedList<int> list = new LinkedList<int>();
//list.Append(1); //list.Append(1);
//list.Append(2); //list.Append(2);
@ -13,8 +14,25 @@ Console.WriteLine("Hello, World!");
//list.Append(4); //list.Append(4);
//list.Append(5); //list.Append(5);
//list.Append(6); //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>(); //DoublyLinkedList<int> list2 = new DoublyLinkedList<int>();
@ -27,27 +45,9 @@ Console.WriteLine("Hello, World!");
//list2.Append(7); //list2.Append(7);
//list2.Append(8); //list2.Append(8);
//list.Insert(9, 5);
//list.Delete(2);
//var x = list[-6]; //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(); Console.ReadLine();