Updated bounds search on Doubly Linked List and completed traverse method

This commit is contained in:
2022-03-10 21:26:16 +00:00
parent bd5c546794
commit 79debf3d62
2 changed files with 44 additions and 20 deletions

View File

@ -33,7 +33,7 @@ namespace C_.Datastructures
get
{
//Check Range
if (i >= Count) throw new System.Exception("Error! Index out of Bounds");
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
//Return Value
DoublyLinkedListNode<T>? node = Traverse(i);
@ -43,13 +43,28 @@ namespace C_.Datastructures
set
{
//Check Range
if (i >= Count) throw new System.Exception("Error! Index out of Bounds");
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()
@ -66,17 +81,30 @@ namespace C_.Datastructures
if (i > (Count/2))
{
//reverse direction of search
direction = -1;
node = Tail;
i = Count - i;
//i becomes the amount of hops left to reach the item
i = Count - i - 1;
}
if (node != null)
{
//continue to end of list
//continue to given point in the list ('i' hops)
for (int x = 0; x < i; x++)
{
//Incomplete Traverse Functions
if (direction == 1)
{//Going forwards
node = node!.Next;
}
else
{
node = node!.Prev;
}
}
}
return node;