Updated Linked List and updated

This commit is contained in:
Luke Else 2022-03-04 16:58:31 +00:00
parent 8165bb849b
commit 00f52ee5c7
5 changed files with 78 additions and 68 deletions

View File

@ -4,9 +4,8 @@ namespace C_.Datastructures
{ {
public class LinkedList<T> public class LinkedList<T>
{ {
public Node<T>? Head { get; set; } = default(Node<T>); internal LinkedListNode<T>? Head { get; set; } = default;
private int Count { get; set; } = 0; private int Count { get; set; } = 0;
public static LinkedList<T> Create(){ public static LinkedList<T> Create(){
//Create a new empty list //Create a new empty list
@ -16,7 +15,7 @@ namespace C_.Datastructures
public static LinkedList<T> Create(T value){ public static LinkedList<T> Create(T value){
//Create a new Class with a single item //Create a new Class with a single item
return new LinkedList<T>(){ return new LinkedList<T>(){
Head = Node<T>.Create(value, null), Head = LinkedListNode<T>.Create(value, null),
Count = 1 Count = 1
}; };
} }
@ -35,7 +34,7 @@ namespace C_.Datastructures
//Find end of list and append fist item of next list //Find end of list and append fist item of next list
if (list2 == null || list.Count == 0) return list; if (list2 == null || list.Count == 0) return list;
Node<T>? end = list.Traverse(); LinkedListNode<T>? end = list.Traverse();
end!.Next = list2!.Head; end!.Next = list2!.Head;
list.Count += list2!.Count; list.Count += list2!.Count;
@ -44,14 +43,15 @@ namespace C_.Datastructures
public T? this[int i] public T? this[int i]
{ {
get { get
{
//Check Range //Check Range
if (i >= Count) throw new System.Exception("Error! Index out of Bounds"); if (i >= Count) throw new System.Exception("Error! Index out of Bounds");
//Return Value //Return Value
Node<T>? node = Traverse(i); LinkedListNode<T>? node = Traverse(i);
if (node != null) return node.Value; if (node != null) return node.Value;
return default(T); return default;
} }
set set
{ {
@ -59,15 +59,13 @@ namespace C_.Datastructures
if (i >= Count) throw new System.Exception("Error! Index out of Bounds"); if (i >= Count) throw new System.Exception("Error! Index out of Bounds");
//Change Value //Change Value
Node<T>? node = Traverse(i); LinkedListNode<T>? node = Traverse(i);
node!.Value = value; node!.Value = value;
} }
} }
public void Append(T value){ public void Append(T value){
Node<T> newItem = Node<T>.Create(value, default(Node<T>)); LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
Count++; Count++;
//Set head to new item if list is empty //Set head to new item if list is empty
@ -78,16 +76,16 @@ namespace C_.Datastructures
} }
//Find last item in list //Find last item in list
Node<T>? end = Head; LinkedListNode<T>? end = Head;
if (end != null) if (end != null)
{ {
end = Traverse(); end = Traverse();
} }
//Append item to end //Append item to end
end!.Next = new Node<T>{ end!.Next = new LinkedListNode<T>{
Value = value, Value = value,
Next = default(Node<T>) Next = default
}; };
} }
@ -96,8 +94,8 @@ namespace C_.Datastructures
if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
//Fetch point in list at which item will be added //Fetch point in list at which item will be added
Node<T>? node = Traverse(index - 1); LinkedListNode<T>? node = Traverse(index - 1);
node!.Next = new Node<T> { node!.Next = new LinkedListNode<T> {
Value = value, Value = value,
Next = node.Next Next = node.Next
}; };
@ -106,60 +104,61 @@ namespace C_.Datastructures
public void Delete(int index) public void Delete(int index)
{ {
//Check if we are trying to reference the first item
if (index == 0 && Head != null) if (index == 0 && Head != null)
{ {
Head = (Node<T>?)Head!.Next; Head = (LinkedListNode<T>?)Head!.Next;
return; return;
} }
Node<T>? node = Traverse(index - 1); //Find node 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"); 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--; Count--;
} }
private Node<T>? Traverse(){ private LinkedListNode<T>? Traverse(){
//Start at Head of list //Start at Head of list
Node<T>? node = Head; LinkedListNode<T>? node = Head;
if (node != null) if (node != null)
{ {
//continue to end of list //continue to end of list
while (node!.Next != default(Node<T>)) while (node!.Next != default)
{ {
node = (Node<T>)node.Next; node = (LinkedListNode<T>)node.Next;
} }
} }
return node; return node;
} }
private Node<T>? Traverse(Node<T> start){ //private static LinkedListNode<T>? Traverse(LinkedListNode<T> start){
//Start at given point in list // //Start at given point in list
Node<T>? node = start; // LinkedListNode<T>? node = start;
if (node != null) // if (node != null)
{ // {
//Continue to end of list // //Continue to end of list
while (node!.Next != default(Node<T>)) // while (node!.Next != default)
{ // {
node = (Node<T>)node.Next; // node = (LinkedListNode<T>)node.Next;
} // }
} // }
return node; // return node;
} //}
private Node<T>? Traverse(int n) private LinkedListNode<T>? Traverse(int n)
{ {
//Start at given point in list //Start at given point in list
Node<T>? node = Head; LinkedListNode<T>? node = Head;
if (node != null || n == 0) if (node != null || n == 0)
{ {
//Continue to end of list //Continue to end of list
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
if (node!.Next == null) return null; if (node!.Next == null) return null;
node = (Node<T>)node.Next; node = (LinkedListNode<T>)node.Next;
} }
} }
return node; return node;

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
public interface INode<T>
{
public T? Value { get; set; }
public INode<T>? Next { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
internal class LinkedListNode<T> : INode<T>
{
public T? Value { get; set; } = default;
public INode<T>? Next { get; set; } = default(LinkedListNode<T>);
public static LinkedListNode<T> Create(T? value, LinkedListNode<T>? next)
{
return new LinkedListNode<T>
{
Value = value,
Next = next
};
}
}
}

View File

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
public interface iNode<T>
{
public T? Value { get; set; }
public iNode<T>? Next { get; set; }
}
public class Node<T> : iNode<T>{
public T? Value { get; set; } = default(T);
public iNode<T>? Next { get; set; } = default(Node<T>);
public static Node<T> Create(T? value, Node<T>? next){
return new Node<T>{
Value = value,
Next = next
};
}
}
}

View File

@ -1,16 +1,17 @@
using System; using System;
using C_.Datastructures; using C_.Datastructures;
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!");
LinkedList<int> list = new LinkedList<int>(); LinkedList<int> list = new();
list.Append(1); list.Append(1);
list.Append(2); list.Append(2);
list.Append(3); list.Append(3);
LinkedList<int> list2 = new LinkedList<int>(); LinkedList<int> list2 = new();
list2.Append(1); list2.Append(1);
list2.Append(2); list2.Append(2);
@ -19,7 +20,7 @@ list2.Append(3);
LinkedList<int> list3 = LinkedList<int>.Create(list, list2); LinkedList<int> list3 = LinkedList<int>.Create(list, list2);
int x = list3[5] = 5;
list3.Insert(4, 1); list3.Insert(4, 1);
list3.Delete(0); list3.Delete(0);
list3.Head = LinkedListNode<int>.Create(5, null);
Console.ReadLine(); Console.ReadLine();