diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs index 0cedaf4..a99a879 100644 --- a/C#/Datastructures/LinkedList.cs +++ b/C#/Datastructures/LinkedList.cs @@ -4,9 +4,8 @@ namespace C_.Datastructures { public class LinkedList { - public Node? Head { get; set; } = default(Node); + internal LinkedListNode? Head { get; set; } = default; private int Count { get; set; } = 0; - public static LinkedList Create(){ //Create a new empty list @@ -16,7 +15,7 @@ namespace C_.Datastructures public static LinkedList Create(T value){ //Create a new Class with a single item return new LinkedList(){ - Head = Node.Create(value, null), + Head = LinkedListNode.Create(value, null), Count = 1 }; } @@ -35,7 +34,7 @@ namespace C_.Datastructures //Find end of list and append fist item of next list if (list2 == null || list.Count == 0) return list; - Node? end = list.Traverse(); + LinkedListNode? end = list.Traverse(); end!.Next = list2!.Head; list.Count += list2!.Count; @@ -44,14 +43,15 @@ namespace C_.Datastructures public T? this[int i] { - get { + get + { //Check Range if (i >= Count) throw new System.Exception("Error! Index out of Bounds"); //Return Value - Node? node = Traverse(i); + LinkedListNode? node = Traverse(i); if (node != null) return node.Value; - return default(T); + return default; } set { @@ -59,15 +59,13 @@ namespace C_.Datastructures if (i >= Count) throw new System.Exception("Error! Index out of Bounds"); //Change Value - Node? node = Traverse(i); + LinkedListNode? node = Traverse(i); node!.Value = value; } } - - public void Append(T value){ - Node newItem = Node.Create(value, default(Node)); + LinkedListNode newItem = LinkedListNode.Create(value, default); Count++; //Set head to new item if list is empty @@ -78,16 +76,16 @@ namespace C_.Datastructures } //Find last item in list - Node? end = Head; + LinkedListNode? end = Head; if (end != null) { end = Traverse(); } //Append item to end - end!.Next = new Node{ + end!.Next = new LinkedListNode{ Value = value, - Next = default(Node) + Next = default }; } @@ -96,8 +94,8 @@ namespace C_.Datastructures if (index > Count) throw new System.Exception("Error! Index outside of Bounds"); //Fetch point in list at which item will be added - Node? node = Traverse(index - 1); - node!.Next = new Node { + LinkedListNode? node = Traverse(index - 1); + node!.Next = new LinkedListNode { Value = value, Next = node.Next }; @@ -106,60 +104,61 @@ namespace C_.Datastructures public void Delete(int index) { + //Check if we are trying to reference the first item if (index == 0 && Head != null) { - Head = (Node?)Head!.Next; + Head = (LinkedListNode?)Head!.Next; return; } - Node? node = Traverse(index - 1); + //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 Node? Traverse(){ + private LinkedListNode? Traverse(){ //Start at Head of list - Node? node = Head; + LinkedListNode? node = Head; if (node != null) { //continue to end of list - while (node!.Next != default(Node)) + while (node!.Next != default) { - node = (Node)node.Next; + node = (LinkedListNode)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; - } + //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 Node? Traverse(int n) + private LinkedListNode? Traverse(int n) { //Start at given point in list - Node? node = Head; + 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 = (Node)node.Next; + node = (LinkedListNode)node.Next; } } return node; diff --git a/C#/Datastructures/Nodes/INode.cs b/C#/Datastructures/Nodes/INode.cs new file mode 100644 index 0000000..11b2f93 --- /dev/null +++ b/C#/Datastructures/Nodes/INode.cs @@ -0,0 +1,13 @@ +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/LinkedListNode.cs b/C#/Datastructures/Nodes/LinkedListNode.cs new file mode 100644 index 0000000..38445ac --- /dev/null +++ b/C#/Datastructures/Nodes/LinkedListNode.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace C_.Datastructures.Nodes +{ + internal class LinkedListNode : INode + { + public T? Value { get; set; } = default; + public INode? Next { get; set; } = default(LinkedListNode); + + public static LinkedListNode Create(T? value, LinkedListNode? next) + { + return new LinkedListNode + { + Value = value, + Next = next + }; + } + } +} diff --git a/C#/Datastructures/Nodes/Node.cs b/C#/Datastructures/Nodes/Node.cs deleted file mode 100644 index 59b8e72..0000000 --- a/C#/Datastructures/Nodes/Node.cs +++ /dev/null @@ -1,26 +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; } - - } - - 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 diff --git a/C#/Program.cs b/C#/Program.cs index a451da0..76989c1 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -1,16 +1,17 @@ using System; using C_.Datastructures; +using C_.Datastructures.Nodes; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); -LinkedList list = new LinkedList(); +LinkedList list = new(); list.Append(1); list.Append(2); list.Append(3); -LinkedList list2 = new LinkedList(); +LinkedList list2 = new(); list2.Append(1); list2.Append(2); @@ -19,7 +20,7 @@ list2.Append(3); LinkedList list3 = LinkedList.Create(list, list2); -int x = list3[5] = 5; list3.Insert(4, 1); list3.Delete(0); +list3.Head = LinkedListNode.Create(5, null); Console.ReadLine(); \ No newline at end of file