diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs new file mode 100644 index 0000000..cb04f29 --- /dev/null +++ b/C#/Datastructures/LinkedList.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using C_.Datastructures.Nodes; + +namespace C_.Datastructures +{ + public class LinkedList + { + public Node? Head { get; set; } = default(Node); + private int Count { get; set; } = 0; + + + public static LinkedList Create(){ + //Create a new empty list + return new LinkedList(); + } + + public static LinkedList Create(T value){ + //Create a new Class with a single item + return new LinkedList(){ + Head = Node.Create(value, null), + Count = 1 + }; + } + + public static LinkedList Create(LinkedList list1, LinkedList list2){ + //Append a previous list to a new List + return null; + } + + + + public void Add(T value){ + Node newItem = Node.Create(value, default(Node)); + Count++; + + //Set head to new item if list is empty + if (Head == null) + { + Head = newItem; + return; + } + + //Find last item in list + Node? end = Head; + if (end != null) + { + end = Traverse(); + } + + //Append item to end + end!.Next = new Node{ + Value = value, + Next = default(Node) + }; + } + + private Node? Traverse(){ + //Start at Head of list + Node? node = Head; + if (node == null) + { + //continue to end of list + while (node!.Next != default(Node)) + { + node = (Node)node.Next; + } + } + return node; + } + + private Node Traverse(Node start){ + //Start at given point in list + Node node = start; + if (node == null) + { + //Continue to end of list + while (node!.Next != default(Node)) + { + node = (Node)node.Next; + } + } + return node; + } + } +} \ No newline at end of file diff --git a/C#/Datastructures/Nodes/Node.cs b/C#/Datastructures/Nodes/Node.cs new file mode 100644 index 0000000..59b8e72 --- /dev/null +++ b/C#/Datastructures/Nodes/Node.cs @@ -0,0 +1,26 @@ +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; } + + } + + public class Node : iNode{ + public T? Value { get; set; } = default(T); + public iNode? Next { get; set; } = default(Node); + + public static Node Create(T? value, Node? next){ + return new Node{ + Value = value, + Next = next + }; + } + } +} \ No newline at end of file