From 88d84ab4483ff7e0a4887c72de400a55459dcdf8 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Thu, 31 Mar 2022 22:13:13 +0100 Subject: [PATCH] Ran Code Cleanup --- C#/Datastructures/DoublyLinkedList.cs | 18 +++++-------- C#/Datastructures/LinkedList.cs | 26 ++++++++++++------- .../Nodes/DoublyLinkedListNode.cs | 8 +----- C#/Datastructures/Nodes/LinkedListNode.cs | 8 +----- C#/Datastructures/Nodes/Node.cs | 8 +----- C#/Datastructures/Nodes/QueueNode.cs | 8 +----- C#/Datastructures/Nodes/StackNode.cs | 5 ---- C#/Datastructures/Queue.cs | 3 +-- C#/Datastructures/Stack.cs | 18 ++++++++----- C#/Program.cs | 2 -- 10 files changed, 40 insertions(+), 64 deletions(-) diff --git a/C#/Datastructures/DoublyLinkedList.cs b/C#/Datastructures/DoublyLinkedList.cs index 257747f..bff020d 100644 --- a/C#/Datastructures/DoublyLinkedList.cs +++ b/C#/Datastructures/DoublyLinkedList.cs @@ -1,8 +1,4 @@ -using System; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using C_.Datastructures.Nodes; +using C_.Datastructures.Nodes; namespace C_.Datastructures { @@ -32,7 +28,7 @@ namespace C_.Datastructures public static DoublyLinkedList Create(DoublyLinkedList list1, DoublyLinkedList list2) { //Create a new list from 2 separate lists - + DoublyLinkedList list; list = list1; @@ -118,7 +114,7 @@ namespace C_.Datastructures node!.Next = DoublyLinkedListNode.Create(value, node.Next, node); //Create backlink in the list - if(node.Next.Next != default) + if (node.Next.Next != default) node.Next.Next.Prev = node.Next; } @@ -131,7 +127,7 @@ namespace C_.Datastructures if (index == 0 && Head != default) { Head = Head!.Next; - if(Head != default) + if (Head != default) Head.Prev = default; return; } @@ -141,7 +137,7 @@ namespace C_.Datastructures { Tail = Tail!.Prev; - if(Tail != default) + if (Tail != default) Tail.Next = default; return; } @@ -153,7 +149,7 @@ namespace C_.Datastructures //Connect item after to the the item before the node we are deleting if (node.Next != default) { - node.Next.Prev = node; + node.Next.Prev = node; } } @@ -170,7 +166,7 @@ namespace C_.Datastructures int direction = 1; DoublyLinkedListNode? node = Head; - if (i > (Count/2)) + if (i > (Count / 2)) { //reverse direction of search direction = -1; diff --git a/C#/Datastructures/LinkedList.cs b/C#/Datastructures/LinkedList.cs index 5876748..7f3b4a7 100644 --- a/C#/Datastructures/LinkedList.cs +++ b/C#/Datastructures/LinkedList.cs @@ -7,23 +7,27 @@ namespace C_.Datastructures internal LinkedListNode? Head { get; set; } = default; private int Count { get; set; } = 0; - public static LinkedList Create(){ + public static LinkedList Create() + { //Create a new empty list return new LinkedList(); } - public static LinkedList Create(T value){ + public static LinkedList Create(T value) + { //Create a new Class with a single item - return new LinkedList(){ + return new LinkedList() + { Head = LinkedListNode.Create(value, null), Count = 1 }; } - public static LinkedList Create(LinkedList list1, LinkedList list2){ + 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) return list2; @@ -52,7 +56,7 @@ namespace C_.Datastructures set { //Check Range - if (i >= Count || i < 0 ) throw new System.Exception("Error! Index out of Bounds"); + if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds"); //Change Value LinkedListNode? node = Traverse(i); @@ -60,13 +64,14 @@ namespace C_.Datastructures } } - public void Append(T value){ + public void Append(T value) + { //Create new node Count++; LinkedListNode newItem = LinkedListNode.Create(value, default); //Set head to new item if list is empty - if (Head == null) + if (Head == null) { Head = newItem; return; @@ -119,11 +124,12 @@ namespace C_.Datastructures node!.Next = node.Next!.Next; } - private LinkedListNode? Traverse(){ + private LinkedListNode? Traverse() + { //Start at Head of list LinkedListNode? node = Head; if (node != null) - { + { //continue to end of list while (node!.Next != default) { diff --git a/C#/Datastructures/Nodes/DoublyLinkedListNode.cs b/C#/Datastructures/Nodes/DoublyLinkedListNode.cs index e46975c..7154cd4 100644 --- a/C#/Datastructures/Nodes/DoublyLinkedListNode.cs +++ b/C#/Datastructures/Nodes/DoublyLinkedListNode.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace C_.Datastructures.Nodes +namespace C_.Datastructures.Nodes { internal class DoublyLinkedListNode : Node> {//Inherits from Node diff --git a/C#/Datastructures/Nodes/LinkedListNode.cs b/C#/Datastructures/Nodes/LinkedListNode.cs index 20ce8ab..225ab5b 100644 --- a/C#/Datastructures/Nodes/LinkedListNode.cs +++ b/C#/Datastructures/Nodes/LinkedListNode.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace C_.Datastructures.Nodes +namespace C_.Datastructures.Nodes { internal class LinkedListNode : Node> {//Inherits from Node diff --git a/C#/Datastructures/Nodes/Node.cs b/C#/Datastructures/Nodes/Node.cs index b4e2ddc..c9dc6b1 100644 --- a/C#/Datastructures/Nodes/Node.cs +++ b/C#/Datastructures/Nodes/Node.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace C_.Datastructures.Nodes +namespace C_.Datastructures.Nodes { internal class Node {//Generic Node type that every other type inherits from diff --git a/C#/Datastructures/Nodes/QueueNode.cs b/C#/Datastructures/Nodes/QueueNode.cs index a37fa02..9bf0054 100644 --- a/C#/Datastructures/Nodes/QueueNode.cs +++ b/C#/Datastructures/Nodes/QueueNode.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace C_.Datastructures.Nodes +namespace C_.Datastructures.Nodes { internal class QueueNode : Node> { diff --git a/C#/Datastructures/Nodes/StackNode.cs b/C#/Datastructures/Nodes/StackNode.cs index 232b6dd..8c5cc68 100644 --- a/C#/Datastructures/Nodes/StackNode.cs +++ b/C#/Datastructures/Nodes/StackNode.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace C_.Datastructures.Nodes { internal class StackNode : Node> diff --git a/C#/Datastructures/Queue.cs b/C#/Datastructures/Queue.cs index 710a4e6..dad1bd4 100644 --- a/C#/Datastructures/Queue.cs +++ b/C#/Datastructures/Queue.cs @@ -1,5 +1,4 @@ -using System.Linq; -using C_.Datastructures.Nodes; +using C_.Datastructures.Nodes; namespace C_.Datastructures { diff --git a/C#/Datastructures/Stack.cs b/C#/Datastructures/Stack.cs index 79859ad..6c5b56b 100644 --- a/C#/Datastructures/Stack.cs +++ b/C#/Datastructures/Stack.cs @@ -7,27 +7,32 @@ namespace C_.Datastructures internal StackNode? Head { get; set; } private int Count { get; set; } = 0; - public static Stack Create(){ + public static Stack Create() + { //Create a new stack without a head return new Stack(); } - public static Stack Create(T value){ + public static Stack Create(T value) + { //Create a new Stack with a head - return new Stack{ + return new Stack + { Head = StackNode.Create(value, default), Count = 1 }; } - public void Push(T value){ + public void Push(T value) + { //Add an Item to the top of the stack Count++; Head = StackNode.Create(value, Head); return; } - public T? Pop(){ + public T? Pop() + { //Take the item off of the top of the stack T? value = default; if (Count > 0) @@ -43,7 +48,8 @@ namespace C_.Datastructures return value; } - public T? Peek(){ + public T? Peek() + { //View item on top of the stack if (Count > 0) { diff --git a/C#/Program.cs b/C#/Program.cs index b516948..c924e7a 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -1,6 +1,4 @@ using System; -using C_.Datastructures; -using C_.Datastructures.Nodes; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!");