2022-04-01 21:02:39 +00:00
|
|
|
|
namespace C_.Datastructures.DoublyLinkedList
|
2022-03-08 21:34:37 +00:00
|
|
|
|
{
|
2022-03-28 22:16:33 +00:00
|
|
|
|
public class DoublyLinkedList<T>
|
2022-03-08 21:34:37 +00:00
|
|
|
|
{
|
|
|
|
|
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>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 20:30:37 +00:00
|
|
|
|
public static DoublyLinkedList<T> Create(T? value)
|
2022-03-08 21:34:37 +00:00
|
|
|
|
{
|
|
|
|
|
//Create a new Class with a single item
|
|
|
|
|
return new DoublyLinkedList<T>()
|
|
|
|
|
{
|
2022-03-16 22:31:54 +00:00
|
|
|
|
Head = DoublyLinkedListNode<T>.Create(value, default, default),
|
2022-03-08 21:34:37 +00:00
|
|
|
|
Count = 1
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 21:37:24 +00:00
|
|
|
|
public static DoublyLinkedList<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2)
|
|
|
|
|
{
|
|
|
|
|
//Create a new list from 2 separate lists
|
2022-03-31 21:13:13 +00:00
|
|
|
|
|
2022-03-15 21:37:24 +00:00
|
|
|
|
DoublyLinkedList<T> list;
|
|
|
|
|
|
|
|
|
|
list = list1;
|
2022-03-16 22:31:54 +00:00
|
|
|
|
if (list == default || list.Count == 0) return list2;
|
2022-03-15 21:37:24 +00:00
|
|
|
|
|
|
|
|
|
//Find end of list and append fist item of next list
|
2022-03-16 22:31:54 +00:00
|
|
|
|
if (list2 == default || list.Count == 0) return list;
|
2022-03-15 21:37:24 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 21:34:37 +00:00
|
|
|
|
public T? this[int i]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
//Check Range
|
2022-03-10 21:26:16 +00:00
|
|
|
|
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
2022-03-08 21:34:37 +00:00
|
|
|
|
|
|
|
|
|
//Return Value
|
|
|
|
|
DoublyLinkedListNode<T>? node = Traverse(i);
|
2022-03-16 22:31:54 +00:00
|
|
|
|
if (node != default) return node.Value;
|
2022-03-08 21:34:37 +00:00
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
//Check Range
|
2022-03-10 21:26:16 +00:00
|
|
|
|
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
2022-03-08 21:34:37 +00:00
|
|
|
|
|
|
|
|
|
//Change Value
|
|
|
|
|
DoublyLinkedListNode<T>? node = Traverse(i);
|
|
|
|
|
node!.Value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-19 20:30:37 +00:00
|
|
|
|
public void Append(T? value)
|
2022-03-10 21:26:16 +00:00
|
|
|
|
{
|
|
|
|
|
Count++;
|
|
|
|
|
|
2022-03-18 22:38:58 +00:00
|
|
|
|
//Set head to new item if list is empty
|
|
|
|
|
if (Head == null)
|
|
|
|
|
{//Append item to front of list (End as well)
|
|
|
|
|
Head = DoublyLinkedListNode<T>.Create(value, default, default);
|
|
|
|
|
Tail = Head;
|
2022-03-10 21:26:16 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 22:38:58 +00:00
|
|
|
|
//Append item to the end of the list
|
|
|
|
|
Tail!.Next = DoublyLinkedListNode<T>.Create(value, default, Tail);
|
|
|
|
|
Tail = Tail.Next;
|
2022-03-10 21:26:16 +00:00
|
|
|
|
}
|
2022-03-08 21:34:37 +00:00
|
|
|
|
|
2022-04-19 20:30:37 +00:00
|
|
|
|
public void Insert(int index, T? value)
|
2022-03-16 22:31:54 +00:00
|
|
|
|
{
|
2022-03-18 22:38:58 +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:38:58 +00:00
|
|
|
|
|
|
|
|
|
//Set head to new item if list is empty
|
|
|
|
|
if (index == 0 || Head == null)
|
|
|
|
|
{
|
|
|
|
|
Head = DoublyLinkedListNode<T>.Create(value, Head, default);
|
|
|
|
|
Tail = Head;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 00:00:13 +00:00
|
|
|
|
//Set tail to new item if index is the end
|
2022-03-23 21:20:19 +00:00
|
|
|
|
if (index == Count - 1)
|
2022-03-19 00:00:13 +00:00
|
|
|
|
{
|
2022-03-23 21:20:19 +00:00
|
|
|
|
//Decrement count as it will be be re-incremented once appended
|
|
|
|
|
Count--;
|
2022-03-19 00:00:13 +00:00
|
|
|
|
Append(value);
|
2022-03-23 21:20:19 +00:00
|
|
|
|
return;
|
2022-03-19 00:00:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 22:31:54 +00:00
|
|
|
|
//Fetch point in list and add new item
|
|
|
|
|
DoublyLinkedListNode<T>? node = Traverse(index - 1);
|
2022-03-18 22:38:58 +00:00
|
|
|
|
node!.Next = DoublyLinkedListNode<T>.Create(value, node.Next, node);
|
2022-03-16 22:31:54 +00:00
|
|
|
|
|
|
|
|
|
//Create backlink in the list
|
2022-03-31 21:13:13 +00:00
|
|
|
|
if (node.Next.Next != default)
|
2022-03-16 22:31:54 +00:00
|
|
|
|
node.Next.Next.Prev = node.Next;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 00:00:13 +00:00
|
|
|
|
public void Delete(int index)
|
|
|
|
|
{
|
|
|
|
|
Count--;
|
|
|
|
|
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
|
|
|
|
|
|
|
|
|
//Check if we are trying to reference the first item
|
|
|
|
|
if (index == 0 && Head != default)
|
|
|
|
|
{
|
|
|
|
|
Head = Head!.Next;
|
2022-03-31 21:13:13 +00:00
|
|
|
|
if (Head != default)
|
2022-03-19 00:00:13 +00:00
|
|
|
|
Head.Prev = default;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-16 22:07:11 +00:00
|
|
|
|
|
2022-03-19 00:00:13 +00:00
|
|
|
|
//Set tail to new item if index is the end
|
|
|
|
|
if (index == Count && Tail != default)
|
|
|
|
|
{
|
|
|
|
|
Tail = Tail!.Prev;
|
2022-03-16 22:07:11 +00:00
|
|
|
|
|
2022-03-31 21:13:13 +00:00
|
|
|
|
if (Tail != default)
|
2022-03-19 00:00:13 +00:00
|
|
|
|
Tail.Next = default;
|
|
|
|
|
return;
|
2022-03-16 22:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 00:00:13 +00:00
|
|
|
|
DoublyLinkedListNode<T>? node = Traverse(index - 1);
|
|
|
|
|
|
|
|
|
|
node!.Next = node.Next!.Next;
|
|
|
|
|
|
2022-03-16 22:07:11 +00:00
|
|
|
|
//Connect item after to the the item before the node we are deleting
|
|
|
|
|
if (node.Next != default)
|
|
|
|
|
{
|
2022-03-31 21:13:13 +00:00
|
|
|
|
node.Next.Prev = node;
|
2022-03-16 22:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 22:20:01 +00:00
|
|
|
|
public int GetCount()
|
|
|
|
|
{//Return the number of items in the list
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
2022-03-08 21:34:37 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2022-03-31 21:13:13 +00:00
|
|
|
|
if (i > (Count / 2))
|
2022-03-08 21:34:37 +00:00
|
|
|
|
{
|
2022-03-10 21:26:16 +00:00
|
|
|
|
//reverse direction of search
|
2022-03-08 21:34:37 +00:00
|
|
|
|
direction = -1;
|
|
|
|
|
node = Tail;
|
2022-03-10 21:26:16 +00:00
|
|
|
|
//i becomes the amount of hops left to reach the item
|
|
|
|
|
i = Count - i - 1;
|
2022-03-08 21:34:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node != null)
|
|
|
|
|
{
|
2022-03-10 21:26:16 +00:00
|
|
|
|
|
|
|
|
|
//continue to given point in the list ('i' hops)
|
2022-03-08 21:34:37 +00:00
|
|
|
|
for (int x = 0; x < i; x++)
|
|
|
|
|
{
|
2022-03-10 21:26:16 +00:00
|
|
|
|
if (direction == 1)
|
|
|
|
|
{//Going forwards
|
|
|
|
|
node = node!.Next;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
node = node!.Prev;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 21:34:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
private DoublyLinkedListNode<T>? Next(DoublyLinkedListNode<T> current)
|
|
|
|
|
{
|
2022-03-16 22:31:54 +00:00
|
|
|
|
if (current != default)
|
2022-03-08 21:34:37 +00:00
|
|
|
|
return current.Next;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current)
|
|
|
|
|
{
|
2022-03-16 22:31:54 +00:00
|
|
|
|
if (current != default)
|
2022-03-08 21:34:37 +00:00
|
|
|
|
return current.Prev;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|