127 lines
3.4 KiB
C#
127 lines
3.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, null, null),
|
|
Count = 1
|
|
};
|
|
}
|
|
|
|
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 != null) 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 Add(T value)
|
|
{
|
|
Count++;
|
|
|
|
if (Tail != null && Count > 0)
|
|
{
|
|
Tail.Next = new DoublyLinkedListNode<T> { Value = value, Next = null, Prev = Tail };
|
|
Tail = Tail.Next;
|
|
return;
|
|
}
|
|
|
|
Head = new DoublyLinkedListNode<T> { Value = value, Next = null, Prev = null };
|
|
Tail = Head;
|
|
|
|
}
|
|
|
|
|
|
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 != null)
|
|
return current.Next;
|
|
|
|
return null;
|
|
}
|
|
|
|
private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current)
|
|
{
|
|
if (current != null)
|
|
return current.Prev;
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|