Created Linked List (incomplete)
This commit is contained in:
parent
bad7445c9b
commit
8945da2569
88
C#/Datastructures/LinkedList.cs
Normal file
88
C#/Datastructures/LinkedList.cs
Normal file
@ -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<T>
|
||||
{
|
||||
public Node<T>? Head { get; set; } = default(Node<T>);
|
||||
private int Count { get; set; } = 0;
|
||||
|
||||
|
||||
public static LinkedList<T> Create(){
|
||||
//Create a new empty list
|
||||
return new LinkedList<T>();
|
||||
}
|
||||
|
||||
public static LinkedList<T> Create(T value){
|
||||
//Create a new Class with a single item
|
||||
return new LinkedList<T>(){
|
||||
Head = Node<T>.Create(value, null),
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public static LinkedList<T> Create(LinkedList<T> list1, LinkedList<T> list2){
|
||||
//Append a previous list to a new List
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Add(T value){
|
||||
Node<T> newItem = Node<T>.Create(value, default(Node<T>));
|
||||
Count++;
|
||||
|
||||
//Set head to new item if list is empty
|
||||
if (Head == null)
|
||||
{
|
||||
Head = newItem;
|
||||
return;
|
||||
}
|
||||
|
||||
//Find last item in list
|
||||
Node<T>? end = Head;
|
||||
if (end != null)
|
||||
{
|
||||
end = Traverse();
|
||||
}
|
||||
|
||||
//Append item to end
|
||||
end!.Next = new Node<T>{
|
||||
Value = value,
|
||||
Next = default(Node<T>)
|
||||
};
|
||||
}
|
||||
|
||||
private Node<T>? Traverse(){
|
||||
//Start at Head of list
|
||||
Node<T>? node = Head;
|
||||
if (node == null)
|
||||
{
|
||||
//continue to end of list
|
||||
while (node!.Next != default(Node<T>))
|
||||
{
|
||||
node = (Node<T>)node.Next;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private Node<T> Traverse(Node<T> start){
|
||||
//Start at given point in list
|
||||
Node<T> node = start;
|
||||
if (node == null)
|
||||
{
|
||||
//Continue to end of list
|
||||
while (node!.Next != default(Node<T>))
|
||||
{
|
||||
node = (Node<T>)node.Next;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
26
C#/Datastructures/Nodes/Node.cs
Normal file
26
C#/Datastructures/Nodes/Node.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_.Datastructures.Nodes
|
||||
{
|
||||
public interface iNode<T>
|
||||
{
|
||||
public T? Value { get; set; }
|
||||
public iNode<T>? Next { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class Node<T> : iNode<T>{
|
||||
public T? Value { get; set; } = default(T);
|
||||
public iNode<T>? Next { get; set; } = default(Node<T>);
|
||||
|
||||
public static Node<T> Create(T? value, Node<T>? next){
|
||||
return new Node<T>{
|
||||
Value = value,
|
||||
Next = next
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user