using C_.Datastructures.Nodes; namespace C_.Datastructures { internal class LinkedList { internal LinkedListNode? Head { get; set; } = default; 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 = LinkedListNode.Create(value, null), Count = 1 }; } public static LinkedList Create(LinkedList list1, LinkedList list2){ //Append a previous list to a new List LinkedList list; list = list1; if (list == null || list.Count == 0) { //If list 2 is the only list present return list2; } //Find end of list and append fist item of next list if (list2 == null || list.Count == 0) return list; LinkedListNode? end = list.Traverse(); end!.Next = list2!.Head; list.Count += list2!.Count; return list; } public T? this[int i] { get { //Check Range if (i >= Count) throw new System.Exception("Error! Index out of Bounds"); //Return Value LinkedListNode? 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 LinkedListNode? node = Traverse(i); node!.Value = value; } } public void Append(T value){ LinkedListNode newItem = LinkedListNode.Create(value, default); Count++; //Set head to new item if list is empty if (Head == null) { Head = newItem; return; } //Find last item in list LinkedListNode? end = Head; if (end != null) { end = Traverse(); } //Append item to end end!.Next = new LinkedListNode{ Value = value, Next = default }; } public void Insert(int index, T value) { if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); //Fetch point in list at which item will be added LinkedListNode? node = Traverse(index - 1); node!.Next = new LinkedListNode { Value = value, Next = node.Next }; Count++; } public void Delete(int index) { //Check if we are trying to reference the first item if (index == 0 && Head != null) { Head = (LinkedListNode?)Head!.Next; return; } //Find node we are trying to delete and then remove / relink LinkedListNode? node = Traverse(index - 1); if (node == null || node.Next == null) throw new System.Exception("Error! Index outside of Bounds"); node.Next = node.Next.Next; Count--; } private LinkedListNode? Traverse(){ //Start at Head of list LinkedListNode? node = Head; if (node != null) { //continue to end of list while (node!.Next != default) { node = (LinkedListNode)node.Next; } } return node; } //private static LinkedListNode? Traverse(LinkedListNode start){ // //Start at given point in list // LinkedListNode? node = start; // if (node != null) // { // //Continue to end of list // while (node!.Next != default) // { // node = (LinkedListNode)node.Next; // } // } // return node; //} private LinkedListNode? Traverse(int n) { //Start at given point in list LinkedListNode? node = Head; if (node != null || n == 0) { //Continue to end of list for (int i = 0; i < n; i++) { if (node!.Next == null) return null; node = (LinkedListNode)node.Next; } } return node; } } }