Updated Delete and Appends methods on linked list
This commit is contained in:
parent
2828466e23
commit
1998e77f18
@ -61,6 +61,7 @@ namespace C_.Datastructures
|
||||
}
|
||||
|
||||
public void Append(T value){
|
||||
//Create new node
|
||||
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
|
||||
Count++;
|
||||
|
||||
@ -79,42 +80,46 @@ namespace C_.Datastructures
|
||||
}
|
||||
|
||||
//Append item to end
|
||||
end!.Next = new LinkedListNode<T>{
|
||||
Value = value,
|
||||
Next = default
|
||||
};
|
||||
end!.Next = newItem;
|
||||
|
||||
}
|
||||
|
||||
public void Insert(int index, T value)
|
||||
{
|
||||
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
|
||||
LinkedListNode<T>? node = Traverse(index - 1);
|
||||
|
||||
node!.Next = new LinkedListNode<T> {
|
||||
Value = value,
|
||||
Next = node.Next
|
||||
};
|
||||
Count++;
|
||||
node!.Next = LinkedListNode<T>.Create(value, node!.Next);
|
||||
}
|
||||
|
||||
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
|
||||
if (index == 0 && Head != null)
|
||||
{
|
||||
Head = (LinkedListNode<T>?)Head!.Next;
|
||||
Head = Head!.Next;
|
||||
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);
|
||||
|
||||
if (node == null || node.Next == null) throw new System.Exception("Error! Index outside of Bounds");
|
||||
|
||||
node.Next = node.Next.Next;
|
||||
Count--;
|
||||
node!.Next = node.Next!.Next;
|
||||
}
|
||||
|
||||
private LinkedListNode<T>? Traverse(){
|
||||
|
@ -5,7 +5,37 @@ using C_.Datastructures.Nodes;
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
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(2);
|
||||
@ -13,25 +43,13 @@ 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.Insert(0, 200);
|
||||
list.Insert(2, 100);
|
||||
list.Delete(2);
|
||||
list.Delete(0);
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
var x = list[-6];
|
||||
|
||||
Console.WriteLine(x);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user