diff --git a/C#/Datastructures/DoublyLinkedList.cs b/C#/Datastructures/DoublyLinkedList.cs new file mode 100644 index 0000000..63d23a0 --- /dev/null +++ b/C#/Datastructures/DoublyLinkedList.cs @@ -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 + { + internal DoublyLinkedListNode? Head { get; set; } = default; + internal DoublyLinkedListNode? Tail { get; set; } = default; + private int Count { get; set; } = 0; + + public static DoublyLinkedList Create() + { + //Create a new empty list + return new DoublyLinkedList(); + } + + public static DoublyLinkedList Create(T value) + { + //Create a new Class with a single item + return new DoublyLinkedList() + { + Head = DoublyLinkedListNode.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? 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? node = Traverse(i); + node!.Value = value; + } + } + + + private DoublyLinkedListNode? Traverse() + { + //Return the final item in the list + return Tail; + } + + private DoublyLinkedListNode? Traverse(int i) + { + //Determine whether to start at the start or end of the list + int direction = 1; + DoublyLinkedListNode? 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? Next(DoublyLinkedListNode current) + { + if (current != null) + return current.Next; + + return null; + } + + private DoublyLinkedListNode? Prev(DoublyLinkedListNode current) + { + if (current != null) + return current.Prev; + + return null; + } + } +} diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs index a99a879..f553ab8 100644 --- a/C#/Datastructures/LinkedList.cs +++ b/C#/Datastructures/LinkedList.cs @@ -2,7 +2,7 @@ using C_.Datastructures.Nodes; namespace C_.Datastructures { - public class LinkedList + internal class LinkedList { internal LinkedListNode? Head { get; set; } = default; private int Count { get; set; } = 0; diff --git a/C#/Datastructures/Nodes/LinkedList/DoublyLinkedListNode.cs b/C#/Datastructures/Nodes/DoublyLinkedListNode.cs similarity index 57% rename from C#/Datastructures/Nodes/LinkedList/DoublyLinkedListNode.cs rename to C#/Datastructures/Nodes/DoublyLinkedListNode.cs index d927c03..e96befd 100644 --- a/C#/Datastructures/Nodes/LinkedList/DoublyLinkedListNode.cs +++ b/C#/Datastructures/Nodes/DoublyLinkedListNode.cs @@ -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 : INode + internal class DoublyLinkedListNode { public T? Value { get; set; } = default; - public INode? Next { get; set; } = default(DoublyLinkedListNode); + public DoublyLinkedListNode? Next { get; set; } = default; - public INode? Prev { get; set; } = default(DoublyLinkedListNode); + public DoublyLinkedListNode? Prev { get; set; } = default; - public static DoublyLinkedListNode Create(T? value, DoublyLinkedListNode? next, DoublyLinkedListNode prev) + public static DoublyLinkedListNode Create(T? value, DoublyLinkedListNode? next, DoublyLinkedListNode? prev) { return new DoublyLinkedListNode { diff --git a/C#/Datastructures/Nodes/ILinkedListNode.cs b/C#/Datastructures/Nodes/ILinkedListNode.cs deleted file mode 100644 index 11b2f93..0000000 --- a/C#/Datastructures/Nodes/ILinkedListNode.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace C_.Datastructures.Nodes -{ - public interface INode - { - public T? Value { get; set; } - public INode? Next { get; set; } - } -} \ No newline at end of file diff --git a/C#/Datastructures/Nodes/LinkedList/LinkedListNode.cs b/C#/Datastructures/Nodes/LinkedListNode.cs similarity index 71% rename from C#/Datastructures/Nodes/LinkedList/LinkedListNode.cs rename to C#/Datastructures/Nodes/LinkedListNode.cs index 60f9535..709071c 100644 --- a/C#/Datastructures/Nodes/LinkedList/LinkedListNode.cs +++ b/C#/Datastructures/Nodes/LinkedListNode.cs @@ -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 : INode + internal class LinkedListNode { public T? Value { get; set; } = default; - public INode? Next { get; set; } = default(LinkedListNode); + public LinkedListNode? Next { get; set; } = default; public static LinkedListNode Create(T? value, LinkedListNode? next) {