Added new functions to DoublyLinkedList - Traverse still incomplete

This commit is contained in:
Luke Else 2022-03-08 21:34:37 +00:00
parent 80bb1cc492
commit 9c1a5738ca
5 changed files with 109 additions and 22 deletions

View File

@ -0,0 +1,100 @@
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) 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) throw new System.Exception("Error! Index out of Bounds");
//Change Value
DoublyLinkedListNode<T>? node = Traverse(i);
node!.Value = value;
}
}
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))
{
direction = -1;
node = Tail;
i = Count - i;
}
if (node != null)
{
//continue to end of list
for (int x = 0; x < i; x++)
{
//Incomplete Traverse Functions
}
}
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;
}
}
}

View File

@ -2,7 +2,7 @@ using C_.Datastructures.Nodes;
namespace C_.Datastructures
{
public class LinkedList<T>
internal class LinkedList<T>
{
internal LinkedListNode<T>? Head { get; set; } = default;
private int Count { get; set; } = 0;

View File

@ -4,16 +4,16 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes.LinkedList
namespace C_.Datastructures.Nodes
{
internal class DoublyLinkedListNode<T> : INode<T>
internal class DoublyLinkedListNode<T>
{
public T? Value { get; set; } = default;
public INode<T>? Next { get; set; } = default(DoublyLinkedListNode<T>);
public DoublyLinkedListNode<T>? Next { get; set; } = default;
public INode<T>? Prev { get; set; } = default(DoublyLinkedListNode<T>);
public DoublyLinkedListNode<T>? Prev { get; set; } = default;
public static DoublyLinkedListNode<T> Create(T? value, DoublyLinkedListNode<T>? next, DoublyLinkedListNode<T> prev)
public static DoublyLinkedListNode<T> Create(T? value, DoublyLinkedListNode<T>? next, DoublyLinkedListNode<T>? prev)
{
return new DoublyLinkedListNode<T>
{

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
public interface INode<T>
{
public T? Value { get; set; }
public INode<T>? Next { get; set; }
}
}

View File

@ -4,12 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes.LinkedList
namespace C_.Datastructures.Nodes
{
internal class LinkedListNode<T> : INode<T>
internal class LinkedListNode<T>
{
public T? Value { get; set; } = default;
public INode<T>? Next { get; set; } = default(LinkedListNode<T>);
public LinkedListNode<T>? Next { get; set; } = default;
public static LinkedListNode<T> Create(T? value, LinkedListNode<T>? next)
{