Ran Code Cleanup

This commit is contained in:
Luke Else 2022-03-31 22:13:13 +01:00
parent fc832edb9d
commit 88d84ab448
10 changed files with 40 additions and 64 deletions

View File

@ -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<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2)
{
//Create a new list from 2 separate lists
DoublyLinkedList<T> list;
list = list1;
@ -118,7 +114,7 @@ namespace C_.Datastructures
node!.Next = DoublyLinkedListNode<T>.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<T>? node = Head;
if (i > (Count/2))
if (i > (Count / 2))
{
//reverse direction of search
direction = -1;

View File

@ -7,23 +7,27 @@ namespace C_.Datastructures
internal LinkedListNode<T>? Head { get; set; } = default;
private int Count { get; set; } = 0;
public static LinkedList<T> Create(){
public static LinkedList<T> Create()
{
//Create a new empty list
return new LinkedList<T>();
}
public static LinkedList<T> Create(T value){
public static LinkedList<T> Create(T value)
{
//Create a new Class with a single item
return new LinkedList<T>(){
return new LinkedList<T>()
{
Head = LinkedListNode<T>.Create(value, null),
Count = 1
};
}
public static LinkedList<T> Create(LinkedList<T> list1, LinkedList<T> list2){
public static LinkedList<T> Create(LinkedList<T> list1, LinkedList<T> list2)
{
//Append a previous list to a new List
LinkedList<T> 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<T>? 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<T> newItem = LinkedListNode<T>.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<T>? Traverse(){
private LinkedListNode<T>? Traverse()
{
//Start at Head of list
LinkedListNode<T>? node = Head;
if (node != null)
{
{
//continue to end of list
while (node!.Next != default)
{

View File

@ -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<T> : Node<T, DoublyLinkedListNode<T>>
{//Inherits from Node

View File

@ -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<T> : Node<T, LinkedListNode<T>>
{//Inherits from Node

View File

@ -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<T, NodeType>
{//Generic Node type that every other type inherits from

View File

@ -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<T> : Node<T, QueueNode<T>>
{

View File

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
internal class StackNode<T> : Node<T, StackNode<T>>

View File

@ -1,5 +1,4 @@
using System.Linq;
using C_.Datastructures.Nodes;
using C_.Datastructures.Nodes;
namespace C_.Datastructures
{

View File

@ -7,27 +7,32 @@ namespace C_.Datastructures
internal StackNode<T>? Head { get; set; }
private int Count { get; set; } = 0;
public static Stack<T> Create(){
public static Stack<T> Create()
{
//Create a new stack without a head
return new Stack<T>();
}
public static Stack<T> Create(T value){
public static Stack<T> Create(T value)
{
//Create a new Stack with a head
return new Stack<T>{
return new Stack<T>
{
Head = StackNode<T>.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<T>.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)
{

View File

@ -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!");