2022-04-01 21:02:39 +00:00
|
|
|
namespace C_.Datastructures.LinkedList
|
2022-03-03 10:12:42 +00:00
|
|
|
{
|
2022-03-28 22:16:33 +00:00
|
|
|
public class LinkedList<T>
|
2022-03-03 10:12:42 +00:00
|
|
|
{
|
2022-03-04 16:58:31 +00:00
|
|
|
internal LinkedListNode<T>? Head { get; set; } = default;
|
2022-03-03 10:12:42 +00:00
|
|
|
private int Count { get; set; } = 0;
|
|
|
|
|
2022-03-31 21:13:13 +00:00
|
|
|
public static LinkedList<T> Create()
|
|
|
|
{
|
2022-03-03 10:12:42 +00:00
|
|
|
//Create a new empty list
|
|
|
|
return new LinkedList<T>();
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:29:25 +00:00
|
|
|
public static LinkedList<T> Create(T? value)
|
2022-03-31 21:13:13 +00:00
|
|
|
{
|
2022-03-03 10:12:42 +00:00
|
|
|
//Create a new Class with a single item
|
2022-03-31 21:13:13 +00:00
|
|
|
return new LinkedList<T>()
|
|
|
|
{
|
2022-03-04 16:58:31 +00:00
|
|
|
Head = LinkedListNode<T>.Create(value, null),
|
2022-03-03 10:12:42 +00:00
|
|
|
Count = 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:13:13 +00:00
|
|
|
public static LinkedList<T> Create(LinkedList<T> list1, LinkedList<T> list2)
|
|
|
|
{
|
2022-03-03 10:12:42 +00:00
|
|
|
//Append a previous list to a new List
|
2022-03-04 08:45:53 +00:00
|
|
|
LinkedList<T> list;
|
2022-03-31 21:13:13 +00:00
|
|
|
|
2022-03-04 08:45:53 +00:00
|
|
|
list = list1;
|
2022-03-15 21:36:57 +00:00
|
|
|
if (list == null || list.Count == 0) return list2;
|
2022-03-04 08:45:53 +00:00
|
|
|
|
|
|
|
//Find end of list and append fist item of next list
|
|
|
|
if (list2 == null || list.Count == 0) return list;
|
|
|
|
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? end = list.Traverse();
|
2022-03-04 08:45:53 +00:00
|
|
|
end!.Next = list2!.Head;
|
|
|
|
list.Count += list2!.Count;
|
|
|
|
|
|
|
|
return list;
|
2022-03-03 10:12:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 10:10:55 +00:00
|
|
|
public T? this[int i]
|
|
|
|
{
|
2022-03-04 16:58:31 +00:00
|
|
|
get
|
|
|
|
{
|
2022-03-04 10:17:51 +00:00
|
|
|
//Check Range
|
2022-03-10 21:24:50 +00:00
|
|
|
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
2022-03-04 10:17:51 +00:00
|
|
|
|
|
|
|
//Return Value
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? node = Traverse(i);
|
2022-03-04 10:10:55 +00:00
|
|
|
if (node != null) return node.Value;
|
2022-03-04 16:58:31 +00:00
|
|
|
return default;
|
2022-03-04 10:10:55 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2022-03-04 10:17:51 +00:00
|
|
|
//Check Range
|
2022-03-31 21:13:13 +00:00
|
|
|
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
2022-03-04 10:17:51 +00:00
|
|
|
|
|
|
|
//Change Value
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? node = Traverse(i);
|
2022-03-04 10:17:51 +00:00
|
|
|
node!.Value = value;
|
2022-03-04 10:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:29:25 +00:00
|
|
|
public void Append(T? value)
|
2022-03-31 21:13:13 +00:00
|
|
|
{
|
2022-03-18 22:28:24 +00:00
|
|
|
//Create new node
|
2022-03-03 10:12:42 +00:00
|
|
|
Count++;
|
2022-03-19 00:00:13 +00:00
|
|
|
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
|
2022-03-03 10:12:42 +00:00
|
|
|
|
|
|
|
//Set head to new item if list is empty
|
2022-03-31 21:13:13 +00:00
|
|
|
if (Head == null)
|
2022-03-03 10:12:42 +00:00
|
|
|
{
|
|
|
|
Head = newItem;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Find last item in list
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? end = Head;
|
2022-03-03 10:12:42 +00:00
|
|
|
if (end != null)
|
|
|
|
{
|
|
|
|
end = Traverse();
|
|
|
|
}
|
2022-03-18 22:28:24 +00:00
|
|
|
|
2022-03-03 10:12:42 +00:00
|
|
|
//Append item to end
|
2022-03-18 22:28:24 +00:00
|
|
|
end!.Next = newItem;
|
2022-03-03 10:12:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 20:29:25 +00:00
|
|
|
public void Insert(int index, T? value)
|
2022-03-04 10:49:08 +00:00
|
|
|
{
|
2022-03-18 22:28:24 +00:00
|
|
|
Count++;
|
2022-03-19 00:00:13 +00:00
|
|
|
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
2022-03-18 22:28:24 +00:00
|
|
|
|
|
|
|
//Set head to new item if list is empty
|
|
|
|
if (index == 0 || Head == null)
|
|
|
|
{
|
|
|
|
Head = LinkedListNode<T>.Create(value, Head);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-04 10:49:08 +00:00
|
|
|
//Fetch point in list at which item will be added
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? node = Traverse(index - 1);
|
2022-03-16 22:31:54 +00:00
|
|
|
|
2022-03-18 22:28:24 +00:00
|
|
|
node!.Next = LinkedListNode<T>.Create(value, node!.Next);
|
2022-03-04 10:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(int index)
|
|
|
|
{
|
2022-03-18 22:28:24 +00:00
|
|
|
Count--;
|
2022-03-19 00:00:13 +00:00
|
|
|
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
2022-03-18 22:28:24 +00:00
|
|
|
|
2022-03-04 16:58:31 +00:00
|
|
|
//Check if we are trying to reference the first item
|
2022-03-19 00:00:13 +00:00
|
|
|
if (index == 0 && Head != default)
|
2022-03-04 10:49:08 +00:00
|
|
|
{
|
2022-03-18 22:28:24 +00:00
|
|
|
Head = Head!.Next;
|
2022-03-04 10:49:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-18 22:28:24 +00:00
|
|
|
//Find node before the one we are trying to delete and then remove / relink
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? node = Traverse(index - 1);
|
2022-03-04 10:49:08 +00:00
|
|
|
|
2022-03-18 22:28:24 +00:00
|
|
|
node!.Next = node.Next!.Next;
|
2022-03-04 10:49:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 22:18:46 +00:00
|
|
|
public int GetCount()
|
|
|
|
{//Return the number of items in the list
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:13:13 +00:00
|
|
|
private LinkedListNode<T>? Traverse()
|
|
|
|
{
|
2022-03-03 10:12:42 +00:00
|
|
|
//Start at Head of list
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? node = Head;
|
2022-03-04 08:45:53 +00:00
|
|
|
if (node != null)
|
2022-03-31 21:13:13 +00:00
|
|
|
{
|
2022-03-03 10:12:42 +00:00
|
|
|
//continue to end of list
|
2022-03-04 16:58:31 +00:00
|
|
|
while (node!.Next != default)
|
2022-03-03 10:12:42 +00:00
|
|
|
{
|
2022-03-04 16:58:31 +00:00
|
|
|
node = (LinkedListNode<T>)node.Next;
|
2022-03-03 10:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2022-03-04 10:10:55 +00:00
|
|
|
|
2022-03-04 16:58:31 +00:00
|
|
|
//private static LinkedListNode<T>? Traverse(LinkedListNode<T> start){
|
|
|
|
// //Start at given point in list
|
|
|
|
// LinkedListNode<T>? node = start;
|
|
|
|
// if (node != null)
|
|
|
|
// {
|
|
|
|
// //Continue to end of list
|
|
|
|
// while (node!.Next != default)
|
|
|
|
// {
|
|
|
|
// node = (LinkedListNode<T>)node.Next;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return node;
|
|
|
|
//}
|
|
|
|
|
2022-03-16 22:07:11 +00:00
|
|
|
private LinkedListNode<T>? Traverse(int i)
|
2022-03-04 10:10:55 +00:00
|
|
|
{
|
|
|
|
//Start at given point in list
|
2022-03-04 16:58:31 +00:00
|
|
|
LinkedListNode<T>? node = Head;
|
2022-03-16 22:07:11 +00:00
|
|
|
if (node != null || i == 0)
|
2022-03-04 10:10:55 +00:00
|
|
|
{
|
|
|
|
//Continue to end of list
|
2022-03-16 22:07:11 +00:00
|
|
|
for (int j = 0; j < i; j++)
|
2022-03-04 10:10:55 +00:00
|
|
|
{
|
2022-03-04 10:49:08 +00:00
|
|
|
if (node!.Next == null) return null;
|
2022-03-04 16:58:31 +00:00
|
|
|
node = (LinkedListNode<T>)node.Next;
|
2022-03-04 10:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2022-03-03 10:12:42 +00:00
|
|
|
}
|
|
|
|
}
|