Compare commits

..

50 Commits

Author SHA1 Message Date
0dfb647f8f Updated repo to be C# only 2022-11-04 12:03:03 +00:00
e2157e7093 Updated Heap to allow Heap Creation (Not fully implemented) 2022-11-04 11:28:52 +00:00
78ac09c4e9 Added Heap Class with Create Methods 2022-09-04 09:16:13 +01:00
a37a81a2bf Updated Program to aid with testing on Doubly Linked List 2022-05-04 21:01:09 +01:00
e66473bb25 Updated Program.cs to test HeapNode weighting 2022-04-20 15:34:43 +01:00
e462929612 Added HeapNode 2022-04-20 15:33:45 +01:00
2056bac6b0 Update Readme 2022-04-20 00:40:41 +01:00
b21efe408b Update Readme 2022-04-20 00:23:52 +01:00
44fa9f8440 Updated Readme 2022-04-19 23:55:20 +01:00
c7ccd641c4 Cleaned up various DataStructures 2022-04-19 22:00:24 +01:00
a8d47f1061 Updated Binary Tree to Include all Traversals
Traversals to be selected using enum
2022-04-19 21:54:54 +01:00
d4291ab9ec Update Queue to allow for null values to be added 2022-04-19 21:31:29 +01:00
a4dfb0f58a Updated Doubly Linked List to allow for null values to be added 2022-04-19 21:30:37 +01:00
e8c99f8d5d Updated Linked List to allow for null value to be added 2022-04-19 21:29:25 +01:00
5360fdd47b Updated Inorder Traversal 2022-04-19 21:09:33 +01:00
a03771cce5 Added GetCount Method to Doubly Linked List 2022-04-17 23:20:01 +01:00
2a9cf2ff4d Added GetCount Method to Linked List 2022-04-17 23:18:46 +01:00
1c5a9c83ce Added GetCount Method to Queue 2022-04-17 23:17:57 +01:00
564e4d161f Re-Written Delete method on Binary Tree 2022-04-17 23:14:18 +01:00
6ae360f3bc Added GetCount method to Stack 2022-04-17 22:17:53 +01:00
88a7a7577e Removed redundant function from Binary Search Tree 2022-04-17 21:48:38 +01:00
e5b0d1659b updated tree deletion method
Still not complete but will occasionally delete correctly, seems to have more issues with items near the top of the tree
2022-04-16 22:36:03 +01:00
4b45f5d561 Added Inorder Traversal 2022-04-14 22:21:40 +01:00
8ac85d8a1f Added 'Delete' method to Binary Tree 2022-04-14 21:55:59 +01:00
1c14ff75d6 Updated Min, Max and Find functions to use stacks 2022-04-14 21:16:41 +01:00
9de49129e7 Updated stack to allow for null references to get added 2022-04-14 20:40:52 +01:00
6906d2e764 Created Max and Min functions for Binary Tree 2022-04-14 20:13:35 +01:00
a2c7326e4b Changes BST to Binary Tree 2022-04-14 17:26:06 +01:00
31888a1529 Updated Tree to ensure no null values are added (Incomparable) and updated descend function to use traverse 2022-04-13 21:17:33 +01:00
9fab683f85 Declared all nodes to be internal 2022-04-13 21:12:18 +01:00
cb09f165f8 Removed whitespace 2022-04-13 21:10:12 +01:00
3b39165316 Completed 'Find' function 2022-04-13 21:08:58 +01:00
3f97e4b770 Fixed CS0019 Error - Cannot assume type T is a reference type but instead can infer that a default node can mean that the tree is empty 2022-04-12 16:03:10 +01:00
702b251a8a Trying to Incorporate Find function but teek getting warnings that Type T cannot be comfirmed as a reference type. (Could be a struct) Need to find oput why it is affecting BST but not Linked List etc... 2022-04-10 22:27:56 +01:00
735bf68e0d Incomplete - Tested 'Find' function; Doesn't always return on the given item but instead will continue past it if it has any children 2022-04-08 22:35:55 +01:00
e9df1cab67 Created 'Add' function for Binary Search Tree 2022-04-05 22:35:33 +01:00
e1a85f7b53 Created Tree + Tree Node 2022-04-04 21:36:04 +01:00
27c51969f8 Updated Generic Nodes to be Uninstantiable 2022-04-04 21:15:12 +01:00
78df73e66d Separeted Generic Nodes (Directed and Undirected) 2022-04-02 21:22:20 +01:00
e5fa390608 Updated Repo Layout 2022-04-01 22:02:39 +01:00
88d84ab448 Ran Code Cleanup 2022-03-31 22:13:13 +01:00
fc832edb9d Added + Completed Queue Datastructure 2022-03-31 22:06:25 +01:00
703826c8a6 Simplified Push Method 2022-03-31 22:05:13 +01:00
c54401fdbb Fixed Push and Pop methods on Stack 2022-03-31 21:55:17 +01:00
1738ef0e5a Created QueueNode 2022-03-31 21:36:43 +01:00
bff4abff01 Stack Completed 2022-03-31 21:32:42 +01:00
40f73375b9 Added comments to StackNode 2022-03-31 21:29:12 +01:00
4fb6d4e15a Updated StackNode to use new Node Type 2022-03-31 21:28:24 +01:00
c69c6ee0d3 Updated DoublyLinkedListNode to use new Node Type 2022-03-31 21:27:31 +01:00
3d5f1cfbc1 Updated LinkedListNode to use new Node Type 2022-03-31 21:24:55 +01:00
23 changed files with 784 additions and 198 deletions

View File

@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
internal class DoublyLinkedListNode<T>
{
public T? Value { get; set; } = default;
public DoublyLinkedListNode<T>? Next { get; set; } = default;
public DoublyLinkedListNode<T>? Prev { get; set; } = default;
public static DoublyLinkedListNode<T> Create(T? value, DoublyLinkedListNode<T>? next, DoublyLinkedListNode<T>? prev)
{
return new DoublyLinkedListNode<T>
{
Value = value,
Next = next,
Prev = prev
};
}
}
}

View File

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
internal class LinkedListNode<T>
{
public T? Value { get; set; } = default;
public LinkedListNode<T>? Next { get; set; } = default;
public static LinkedListNode<T> Create(T? value, LinkedListNode<T>? next)
{
return new LinkedListNode<T>
{
Value = value,
Next = next
};
}
}
}

View File

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
internal class Node<T, NodeType>
{
public T? Value { get; set; } = default;
public NodeType? Next { get; set; } = default;
}
}

View File

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace C_.Datastructures.Nodes
{
internal class StackNode<T>
{
public T? Value { get; set; } = default;
public StackNode<T>? Next { get; set; } = default;
public static StackNode<T> Create(T? value, StackNode<T>? next)
{
return new StackNode<T>
{
Value = value,
Next = next
};
}
}
}

View File

@ -1,50 +0,0 @@
using System;
using C_.Datastructures;
using C_.Datastructures.Nodes;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
//LinkedList<int> list = new LinkedList<int>();
//list.Append(1);
//list.Append(2);
//list.Append(3);
//list.Append(4);
//list.Append(5);
//list.Append(6);
//list.Delete(5);
//list.Delete(2);
//list.Delete(0);
// DoublyLinkedList<int> list = new DoublyLinkedList<int>();
// list.Append(1);
// list.Append(2);
// list.Append(3);
// list.Append(4);
// list.Append(5);
// list.Append(6);
// list.Insert(6, 1);
// Console.Write($"{list[0]} ");
// Console.Write($"{list[1]} ");
// Console.Write($"{list[2]} ");
// Console.Write($"{list[3]} ");
// Console.Write($"{list[4]} ");
// Console.Write($"{list[5]} ");
// Console.Write($"{list[6]} \n");
Stack<int> test = Stack<int>.Create();
Console.ReadLine();

View File

@ -0,0 +1,286 @@
using C_.Datastructures.LinkedList;
using C_.Datastructures.Stack;
using C_.Datastructures.Queue;
using System;
namespace C_.Datastructures.BinaryTree
{
internal class Tree<T> where T:IComparable
{
public TreeNode<T>? Root { get; set; }
public int Count { get; set; }
public static Tree<T> Create(){
//Create a new Tree with no Head
return new Tree<T>{
Root = null,
Count = 0
};
}
public static Tree<T> Create(T value){
//Create a new Tree with Head
return new Tree<T>{
Root = TreeNode<T>.Create(value)
};
}
public void Add(T value)
{//Add item to the correct position in the tree (Input cannot be null)
Count++;
if (Root == default)
{//If new node should become the root
Root = TreeNode<T>.Create(value);
return;
}
//Find position to insert
TreeNode<T> node = Root;
node = Descend(value, node)!;
if (value.CompareTo(node.Value) < 0)
{//Insert to left
node.Left = TreeNode<T>.Create(value);
return;
}
//Insert to right
node.Right = TreeNode<T>.Create(value);
return;
}
public bool Delete(T value)
{
//Check if root of tree is null
if (Count == 0)
return false;
//Check if the only value is the root
if (Count == 1)
{
if (Root!.Value!.Equals(value))
{//If the only item is the one we are trying to delete
Count = 0;
Root = default;
return true;
}
return false;
}
//Stack to store the items leading up to and including the one we are trying to delete
Stack<TreeNode<T>>? deletionStack;
//Search for item being deleted + Parents
deletionStack = Find(value);
if (deletionStack == default)
return false; //Item was not found
//Delete Item (replace) and replace pointers to retain integrity of tree
TreeNode<T>? node = deletionStack.Pop();
//stack to store the items leading up to the value that we will use to replace the node
Stack<TreeNode<T>>? replacementStack = Min(node!.Right);
if (replacementStack == default)
{//Nothing to the right of the value we are deleting
if (deletionStack.Peek() != default)
{//Parent adopts left hand side of node if present
deletionStack.Pop()!.Left = node.Left;
}
if (node.Left != default)
{//Node adopts left value if no lower value to the right
node.Value = node.Left!.Value;
node.Left = node.Left.Left;
}
}
else
{//Replace the value + reorder nodes
node.Value = replacementStack.Peek()!.Value;
TreeNode<T>? replacementNode = replacementStack.Pop();
switch (replacementStack.GetCount())
{//Determine what to do based on number of items in replacement stack
case 1:
node.Right = replacementNode!.Right;
break;
case >=2:
replacementStack.Peek()!.Left = replacementNode!.Right;
break;
default:
break;
}
}
Count--;
return true;
}
public LinkedList<T>? Traverse(TraversalType traversalType)
{
if (Root == default)
return default;
LinkedList<T> list = LinkedList<T>.Create();
switch (traversalType)
{//Select which type of traversal to do
case TraversalType.Inorder:
Inorder(list, Root);
break;
case TraversalType.Preorder:
Preorder(list, Root);
break;
case TraversalType.Postorder:
Postorder(list, Root);
break;
case TraversalType.Breadth:
Queue<TreeNode<T>> queue = Queue<TreeNode<T>>.Create();
BreadthFirst(list, queue, Root);
break;
default:
return default;
}
return list;
}
private void Inorder(LinkedList<T> list, TreeNode<T> node)
{//Inorder Traversal
if (node.Left != default)
Inorder(list, node.Left);
list.Append(node.Value);
if (node.Right != default)
Inorder(list, node.Right);
}
private void Preorder(LinkedList<T> list, TreeNode<T> node)
{//Preorder Traversal
list.Append(node.Value);
if (node.Left != default)
Preorder(list, node.Left);
if (node.Right != default)
Preorder(list, node.Right);
}
private void Postorder(LinkedList<T> list, TreeNode<T> node)
{//Postorder Traversal
if (node.Left != default)
Postorder(list, node.Left);
if (node.Right != default)
Postorder(list, node.Right);
list.Append(node.Value);
}
private void BreadthFirst(LinkedList<T> list, Queue<TreeNode<T>> queue, TreeNode<T> node)
{//Breadth First Traversal
list.Append(node.Value);
if (node.Left != default)
queue.Push(node.Left);
if (node.Right != default)
queue.Push(node.Right);
//Only continue to traverse if there are no mode nodes to process
if (queue.Peek() != default)
BreadthFirst(list, queue, queue.Pop()!);
}
private static TreeNode<T>? GetNext(T value, TreeNode<T>? node)
{//T is comparable so use methods to determine which way to traverse
if(node == default)
return default;
if (value.CompareTo(node.Value) < 0)
{//Traverse Left
return node.Left;
}
//Traverse Right
return node.Right;
}
private Stack<TreeNode<T>>? Find(T value)
{//Return true if the item can be found within the tree
if (Root == default || Root.Value!.Equals(default))
return default;
Stack<TreeNode<T>>? stack = Stack<TreeNode<T>>.Create(Root);
while (stack.Peek() != default)
{
//Compare value at node to see if we are looking for the root item
if (stack.Peek()!.Value!.Equals(value))
return stack;
stack.Push(GetNext(value, stack.Peek()));
}
return default;
}
private static Stack<TreeNode<T>>? Min(TreeNode<T>? node)
{//Returns a Stack with the value on top being the minimum of the subtree
if(node == default)
return default;
//Stack to store and be able to get the parent values
Stack<TreeNode<T>> stack = Stack<TreeNode<T>>.Create(node);
while(true){
if (stack.Peek()!.Left == default)
return stack;
stack.Push(stack.Peek()!.Left);
}
}
private static Stack<TreeNode<T>>? Max(TreeNode<T>? node)
{///Returns a Stack with the value on top being the maximum of the subtree
if(node == default)
return default;
//Stack to store and be able to get the parent values
Stack<TreeNode<T>> stack = Stack<TreeNode<T>>.Create(node);
while(true){
if (stack.Peek()!.Right == default)
return stack;
stack.Push(stack.Peek()!.Right);
}
}
private TreeNode<T>? Descend(T value, TreeNode<T>? current)
{//Keep trying to determine whether to go left or right until null node is found that can be appended to
if (current == default)
return default;
TreeNode<T>? node;
node = Descend(value, GetNext(value, current));
if (node == null)
{
return current;
}
return node;
}
}
public enum TraversalType
{//Enum to allow for Traversal selection
Inorder,
Preorder,
Postorder,
Breadth
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using C_.Datastructures.Generic;
namespace C_.Datastructures.BinaryTree
{
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
{
//All properties inherited from base class
public static TreeNode<T> Create(T value){
//Create a new node without any children
return new TreeNode<T>{
Value = value
};
}
public static TreeNode<T> Create(T value, TreeNode<T>? left, TreeNode<T>? right){
//Create a new node with the option of having children
return new TreeNode<T>{
Value = value,
Left = left,
Right = right
};
}
}
}

View File

@ -1,10 +1,4 @@
using System; namespace C_.Datastructures.DoublyLinkedList
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using C_.Datastructures.Nodes;
namespace C_.Datastructures
{ {
public class DoublyLinkedList<T> public class DoublyLinkedList<T>
{ {
@ -18,7 +12,7 @@ namespace C_.Datastructures
return new DoublyLinkedList<T>(); return new DoublyLinkedList<T>();
} }
public static DoublyLinkedList<T> Create(T value) public static DoublyLinkedList<T> Create(T? value)
{ {
//Create a new Class with a single item //Create a new Class with a single item
return new DoublyLinkedList<T>() return new DoublyLinkedList<T>()
@ -28,7 +22,6 @@ namespace C_.Datastructures
}; };
} }
public static DoublyLinkedList<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2) public static DoublyLinkedList<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2)
{ {
//Create a new list from 2 separate lists //Create a new list from 2 separate lists
@ -74,7 +67,7 @@ namespace C_.Datastructures
node!.Value = value; node!.Value = value;
} }
} }
public void Append(T value) public void Append(T? value)
{ {
Count++; Count++;
@ -91,7 +84,7 @@ namespace C_.Datastructures
Tail = Tail.Next; Tail = Tail.Next;
} }
public void Insert(int index, T value) public void Insert(int index, T? value)
{ {
Count++; Count++;
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds"); if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
@ -157,6 +150,10 @@ namespace C_.Datastructures
} }
} }
public int GetCount()
{//Return the number of items in the list
return Count;
}
private DoublyLinkedListNode<T>? Traverse() private DoublyLinkedListNode<T>? Traverse()
{ {

View File

@ -0,0 +1,19 @@
using C_.Datastructures.Generic;
namespace C_.Datastructures.DoublyLinkedList
{
internal class DoublyLinkedListNode<T> : UndirectedNode<T, DoublyLinkedListNode<T>>
{//Inherits from Node
internal DoublyLinkedListNode<T>? Prev { get; set; } = default;
public static DoublyLinkedListNode<T> Create(T? value, DoublyLinkedListNode<T>? next, DoublyLinkedListNode<T>? prev)
{
return new DoublyLinkedListNode<T>
{
Value = value,
Next = next,
Prev = prev
};
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_.Datastructures.Generic
{
abstract internal class DirectedNode<T, NodeType>
{
public T? Value { get; set; } = default;
internal NodeType? Left { get; set; } = default;
internal NodeType? Right { get; set; } = default;
}
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace C_.Datastructures.Generic
{
abstract internal class UndirectedNode<T, NodeType>
{//Generic Node type that every other type inherits from
public T? Value { get; set; } = default;
internal NodeType? Next { get; set; } = default;
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace C_.Datastructures.Heap
{
internal class Heap<T> where T:IComparable
{
internal HeapNode<T>? Root { get; set; }
private int Count { get; set; }
public static Heap<T> Create(){
return new Heap<T>{
Root = null,
Count = 0
};
}
public static Heap<T> Create(T value){
return new Heap<T>{
Root = HeapNode<T>.Create(value),
Count = 1
};
}
public void Add(T value){
Count++;
if (Root == default)
{//If the new node needs to be added to the top of the heap
Root = HeapNode<T>.Create(value);
return;
}
//If the new node can be placed in a subchild
HeapNode<T> node = Root;
while(node.Left != default){
node = node.Left;
}
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C_.Datastructures.Generic;
using System.Threading.Tasks;
namespace C_.Datastructures.Heap
{
internal class HeapNode<T> : DirectedNode<T, HeapNode<T>>
{
internal int LeftWeight { get; set; }
internal int RightWeight { get; set; }
//All properties inherited from base class
public static HeapNode<T> Create(T value)
{
//Create a new node without any children
return new HeapNode<T>
{
Value = value
};
}
public static HeapNode<T> Create(T value, HeapNode<T>? left, HeapNode<T>? right)
{
//Create a new node with the option of having children
return new HeapNode<T>
{
Value = value,
Left = left,
LeftWeight = (left != default) ? 1 : 0,
Right = right,
RightWeight = (right != default) ? 1 : 0
};
}
}
}

View File

@ -1,26 +1,28 @@
using C_.Datastructures.Nodes; namespace C_.Datastructures.LinkedList
namespace C_.Datastructures
{ {
public class LinkedList<T> public class LinkedList<T>
{ {
internal LinkedListNode<T>? Head { get; set; } = default; internal LinkedListNode<T>? Head { get; set; } = default;
private int Count { get; set; } = 0; private int Count { get; set; } = 0;
public static LinkedList<T> Create(){ public static LinkedList<T> Create()
{
//Create a new empty list //Create a new empty list
return new LinkedList<T>(); 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 //Create a new Class with a single item
return new LinkedList<T>(){ return new LinkedList<T>()
{
Head = LinkedListNode<T>.Create(value, null), Head = LinkedListNode<T>.Create(value, null),
Count = 1 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 //Append a previous list to a new List
LinkedList<T> list; LinkedList<T> list;
@ -60,7 +62,8 @@ namespace C_.Datastructures
} }
} }
public void Append(T value){ public void Append(T? value)
{
//Create new node //Create new node
Count++; Count++;
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default); LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
@ -83,7 +86,7 @@ namespace C_.Datastructures
end!.Next = newItem; end!.Next = newItem;
} }
public void Insert(int index, T value) public void Insert(int index, T? value)
{ {
Count++; Count++;
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds"); if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
@ -119,7 +122,13 @@ namespace C_.Datastructures
node!.Next = node.Next!.Next; node!.Next = node.Next!.Next;
} }
private LinkedListNode<T>? Traverse(){ public int GetCount()
{//Return the number of items in the list
return Count;
}
private LinkedListNode<T>? Traverse()
{
//Start at Head of list //Start at Head of list
LinkedListNode<T>? node = Head; LinkedListNode<T>? node = Head;
if (node != null) if (node != null)
@ -133,20 +142,6 @@ namespace C_.Datastructures
return node; return node;
} }
//private static LinkedListNode<T>? Traverse(LinkedListNode<T> start){
// //Start at given point in list
// LinkedListNode<T>? node = start;
// if (node != null)
// {
// //Continue to end of list
// while (node!.Next != default)
// {
// node = (LinkedListNode<T>)node.Next;
// }
// }
// return node;
//}
private LinkedListNode<T>? Traverse(int i) private LinkedListNode<T>? Traverse(int i)
{ {
//Start at given point in list //Start at given point in list

View File

@ -0,0 +1,16 @@
using C_.Datastructures.Generic;
namespace C_.Datastructures.LinkedList
{
internal class LinkedListNode<T> : UndirectedNode<T, LinkedListNode<T>>
{//Inherits from Node
public static LinkedListNode<T> Create(T? value, LinkedListNode<T>? next)
{
return new LinkedListNode<T>
{
Value = value,
Next = next
};
}
}
}

View File

@ -0,0 +1,75 @@
namespace C_.Datastructures.Queue
{
internal class Queue<T>
{
internal QueueNode<T>? Head { get; set; }
internal QueueNode<T>? Tail { get; set; }
private int Count { get; set; } = 0;
public static Queue<T> Create()
{
//Create a new queue without a head / tail
return new Queue<T>();
}
public static Queue<T> Create(T? value)
{
//Create a new Queue with a head + tail
QueueNode<T> newNode = QueueNode<T>.Create(value, default);
return new Queue<T>
{
Head = newNode,
Tail = newNode,
Count = 1
};
}
public void Push(T? value)
{
//Add an Item to the end of the Queue
Count++;
if (Count > 1)
{
Tail!.Next = QueueNode<T>.Create(value, default);
Tail = Tail!.Next;
return;
}
Head = QueueNode<T>.Create(value, default);
Tail = Head;
return;
}
public T? Pop()
{
//Take the item off the front of the queue
T? value = default;
if (Count > 0)
{//Assign the default value if there are any items left in the Queue
Count--;
value = Head!.Value;
Head = Head.Next;
if (Count == 0)
{
Head = default;
Tail = Head;
}
}
return value;
}
public T? Peek()
{
//View item at the front of the Queue
if (Count > 0)
{
return Head!.Value;
}
return default;
}
public int GetCount()
{//Return the number of items in the list
return Count;
}
}
}

View File

@ -0,0 +1,16 @@
using C_.Datastructures.Generic;
namespace C_.Datastructures.Queue
{
internal class QueueNode<T> : UndirectedNode<T, QueueNode<T>>
{
public static QueueNode<T> Create(T? value, QueueNode<T>? next)
{
return new QueueNode<T>
{
Value = value,
Next = next
};
}
}
}

View File

@ -1,52 +1,64 @@
using C_.Datastructures.Nodes; namespace C_.Datastructures.Stack
namespace C_.Datastructures
{ {
public class Stack<T> public class Stack<T>
{ {
internal StackNode<T>? Head { get; set; } internal StackNode<T>? Head { get; set; }
private int Count { get; set; } = 0; private int Count { get; set; } = 0;
public static Stack<T> Create(){ public static Stack<T> Create()
{
//Create a new stack without a head //Create a new stack without a head
return new Stack<T>(); 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 //Create a new Stack with a head
return new Stack<T>{ return new Stack<T>
{
Head = StackNode<T>.Create(value, default), Head = StackNode<T>.Create(value, default),
Count = 1 Count = 1
}; };
} }
public void Push(T value){ public void Push(T? value)
//Add an Item to the end of the list
if (Count > 0)
{ {
//Add an Item to the top of the stack
Count++; Count++;
Head = StackNode<T>.Create(value, Head); Head = StackNode<T>.Create(value, Head);
return; return;
} }
}
public T? Pop(){ public T? Pop()
{
//Take the item off of the top of the stack
T? value = default; T? value = default;
if (Count > 0) if (Count > 0)
{//Assign the default value if there are any items left in the stack {//Assign the default value if there are any items left in the stack
Count--; Count--;
value = Head!.Value; value = Head!.Value;
Head = Head.Next; Head = Head.Next;
if (Count == 0)
{
Head = default;
}
} }
return value; return value;
} }
public T? Peek(){ public T? Peek()
{
//View item on top of the stack
if (Count > 0) if (Count > 0)
{ {
return Head!.Value; return Head!.Value;
} }
return default; return default;
} }
public int GetCount()
{//Return the number of items in the list
return Count;
}
} }
} }

View File

@ -0,0 +1,16 @@
using C_.Datastructures.Generic;
namespace C_.Datastructures.Stack
{
internal class StackNode<T> : UndirectedNode<T, StackNode<T>>
{//Inherits from Node
public static StackNode<T> Create(T? value, StackNode<T>? next)
{
return new StackNode<T>
{
Value = value,
Next = next
};
}
}
}

153
Program.cs Normal file
View File

@ -0,0 +1,153 @@
using System;
using C_.Datastructures.DoublyLinkedList;
using C_.Datastructures.BinaryTree;
using C_.Datastructures.Heap;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
//LinkedList<int> list = new LinkedList<int>();
//list.Append(1);
//list.Append(2);
//list.Append(3);
//list.Append(4);
//list.Append(5);
//list.Append(6);
//list.Delete(5);
//list.Delete(2);
//list.Delete(0);
// DoublyLinkedList<int> list = new DoublyLinkedList<int>();
// list.Append(1);
// list.Append(2);
// list.Append(3);
// list.Append(4);
// list.Append(5);
// list.Append(6);
// list.Insert(6, 1);
// list.Delete(0);
// Console.Write($"{list[0]} ");
// Console.Write($"{list[1]} ");
// Console.Write($"{list[2]} ");
// Console.Write($"{list[3]} ");
// Console.Write($"{list[4]} ");
// Console.Write($"{list[5]} ");
// Console.Write($"{list[6]} \n");
//Stack<int> stack = Stack<int>.Create();
//stack.Push(1);
//stack.Push(2);
//stack.Push(3);
//stack.Push(4);
//stack.Push(5);
//stack.Push(6);
//Console.WriteLine(stack.Peek());
//stack.Pop();
//stack.Push(7);
//stack.Pop();
//stack.Pop();
//stack.Pop();
//stack.Pop();
//stack.Pop();
//stack.Pop();
//Queue<int> queue = Queue<int>.Create();
//queue.Push(1);
//queue.Push(2);
//queue.Push(3);
//queue.Push(4);
//queue.Push(5);
//queue.Push(6);
//Console.WriteLine(queue.Peek());
//queue.Pop();
//queue.Push(7);
//queue.Pop();
//queue.Pop();
//queue.Pop();
//queue.Pop();
//queue.Pop();
//queue.Pop();
//Tree<int> tree = new Tree<int>();
//tree.Add(40);
//tree.Add(30);
//tree.Add(50);
//tree.Add(25);
//tree.Add(35);
//tree.Add(45);
//tree.Add(60);
//tree.Add(15);
//tree.Add(28);
//tree.Add(55);
//tree.Add(70);
////var x = tree.Delete(10);
////tree.Delete(5);
//var inorder = tree.Traverse(TraversalType.Inorder);
//var preorder = tree.Traverse(TraversalType.Preorder);
//var postorder = tree.Traverse(TraversalType.Postorder);
//var breadthFirst = tree.Traverse(TraversalType.Breadth);
var x = HeapNode<int>.Create(1, null, new HeapNode<int> { Left = default, Right = default });
Console.ReadLine();

View File

@ -1,6 +1,6 @@
# DataStructures # DataStructures
List of DataStructures implemented in C#, Go & Rust List of DataStructures implemented in C#
## What is Included ## What is Included
@ -10,8 +10,8 @@ Linked List
Doubly Linked List Doubly Linked List
Stack Stack
Queue Queue
Binary Search Tree Binary Tree
Heap Heap ~
### Algorithms ### Algorithms

View File