Updated Delete and Appends methods on linked list

This commit is contained in:
Luke Else 2022-03-18 22:28:24 +00:00
parent 2828466e23
commit 1998e77f18
2 changed files with 57 additions and 34 deletions

View File

@ -61,6 +61,7 @@ namespace C_.Datastructures
} }
public void Append(T value){ public void Append(T value){
//Create new node
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default); LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
Count++; Count++;
@ -77,44 +78,48 @@ namespace C_.Datastructures
{ {
end = Traverse(); end = Traverse();
} }
//Append item to end //Append item to end
end!.Next = new LinkedListNode<T>{ end!.Next = newItem;
Value = value,
Next = default
};
} }
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"); 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<T>.Create(value, Head);
return;
}
//Fetch point in list at which item will be added //Fetch point in list at which item will be added
LinkedListNode<T>? node = Traverse(index - 1); LinkedListNode<T>? node = Traverse(index - 1);
node!.Next = new LinkedListNode<T> { node!.Next = LinkedListNode<T>.Create(value, node!.Next);
Value = value,
Next = node.Next
};
Count++;
} }
public void Delete(int index) 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 //Check if we are trying to reference the first item
if (index == 0 && Head != null) if (index == 0 && Head != null)
{ {
Head = (LinkedListNode<T>?)Head!.Next; Head = Head!.Next;
return; 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<T>? node = Traverse(index - 1); LinkedListNode<T>? node = Traverse(index - 1);
if (node == null || node.Next == null) throw new System.Exception("Error! Index outside of Bounds"); node!.Next = node.Next!.Next;
node.Next = node.Next.Next;
Count--;
} }
private LinkedListNode<T>? Traverse(){ private LinkedListNode<T>? Traverse(){

View File

@ -5,7 +5,37 @@ 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>(); //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.Append(7);
//list.Append(8);
//DoublyLinkedList<int> list2 = new DoublyLinkedList<int>();
//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<int> list = new LinkedList<int>();
list.Append(1); list.Append(1);
list.Append(2); list.Append(2);
@ -13,25 +43,13 @@ list.Append(3);
list.Append(4); list.Append(4);
list.Append(5); list.Append(5);
list.Append(6); list.Append(6);
list.Append(7);
list.Append(8);
DoublyLinkedList<int> list2 = new DoublyLinkedList<int>();
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(2);
list.Delete(0);
Console.ReadLine();
var x = list[-6];
Console.WriteLine(x);