DataStructuresCSharp/C#/Datastructures/DoublyLinkedList.cs

191 lines
5.4 KiB
C#

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using C_.Datastructures.Nodes;
namespace C_.Datastructures
{
internal class DoublyLinkedList<T>
{
internal DoublyLinkedListNode<T>? Head { get; set; } = default;
internal DoublyLinkedListNode<T>? Tail { get; set; } = default;
private int Count { get; set; } = 0;
public static DoublyLinkedList<T> Create()
{
//Create a new empty list
return new DoublyLinkedList<T>();
}
public static DoublyLinkedList<T> Create(T value)
{
//Create a new Class with a single item
return new DoublyLinkedList<T>()
{
Head = DoublyLinkedListNode<T>.Create(value, default, default),
Count = 1
};
}
public static DoublyLinkedList<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2)
{
//Create a new list from 2 separate lists
DoublyLinkedList<T> list;
list = list1;
if (list == default || list.Count == 0) return list2;
//Find end of list and append fist item of next list
if (list2 == default || list.Count == 0) return list;
DoublyLinkedListNode<T>? end = list.Traverse();
//Connect up pointers at ajoining section
end!.Next = list2!.Head;
end!.Next!.Prev = end;
end = list2.Tail;
list.Count += list2!.Count;
return list;
}
public T? this[int i]
{
get
{
//Check Range
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
//Return Value
DoublyLinkedListNode<T>? node = Traverse(i);
if (node != default) return node.Value;
return default;
}
set
{
//Check Range
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
//Change Value
DoublyLinkedListNode<T>? node = Traverse(i);
node!.Value = value;
}
}
public void Append(T value)
{
Count++;
if (Tail != null && Count > 0)
{
Tail.Next = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = Tail };
Tail = Tail.Next;
return;
}
Head = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = default };
Tail = Head;
}
public void Insert(int index, T value)
{
if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
//Fetch point in list and add new item
DoublyLinkedListNode<T>? node = Traverse(index - 1);
node!.Next = new DoublyLinkedListNode<T>{
Value = value,
Next = node.Next,
Prev = node
};
//Create backlink in the list
if(node.Next.Next != default)
node.Next.Next.Prev = node.Next;
Count++;
}
public bool Delete(int index){
DoublyLinkedListNode<T>? node = Traverse(index);
if (node == default) return false;
if(node.Prev != default){
//connect item beofre to item after node we are deleting
node.Prev.Next = node.Next;
}
//Connect item after to the the item before the node we are deleting
if (node.Next != default)
{
node.Next.Prev = node.Prev;
}
Count--;
return true;
}
private DoublyLinkedListNode<T>? Traverse()
{
//Return the final item in the list
return Tail;
}
private DoublyLinkedListNode<T>? Traverse(int i)
{
//Determine whether to start at the start or end of the list
int direction = 1;
DoublyLinkedListNode<T>? node = Head;
if (i > (Count/2))
{
//reverse direction of search
direction = -1;
node = Tail;
//i becomes the amount of hops left to reach the item
i = Count - i - 1;
}
if (node != null)
{
//continue to given point in the list ('i' hops)
for (int x = 0; x < i; x++)
{
if (direction == 1)
{//Going forwards
node = node!.Next;
}
else
{
node = node!.Prev;
}
}
}
return node;
}
private DoublyLinkedListNode<T>? Next(DoublyLinkedListNode<T> current)
{
if (current != default)
return current.Next;
return null;
}
private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current)
{
if (current != default)
return current.Prev;
return null;
}
}
}