Updated repo to be C# only
This commit is contained in:
		
							
								
								
									
										64
									
								
								Datastructures/Stack/Stack.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								Datastructures/Stack/Stack.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
namespace C_.Datastructures.Stack
 | 
			
		||||
{
 | 
			
		||||
    public class Stack<T>
 | 
			
		||||
    {
 | 
			
		||||
        internal StackNode<T>? Head { get; set; }
 | 
			
		||||
        private int Count { get; set; } = 0;
 | 
			
		||||
 | 
			
		||||
        public static Stack<T> Create()
 | 
			
		||||
        {
 | 
			
		||||
            //Create a new stack without a head
 | 
			
		||||
            return new Stack<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static Stack<T> Create(T? value)
 | 
			
		||||
        {
 | 
			
		||||
            //Create a new Stack with a head
 | 
			
		||||
            return new Stack<T>
 | 
			
		||||
            {
 | 
			
		||||
                Head = StackNode<T>.Create(value, default),
 | 
			
		||||
                Count = 1
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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()
 | 
			
		||||
        {
 | 
			
		||||
            //Take the item off of the top of the stack
 | 
			
		||||
            T? value = default;
 | 
			
		||||
            if (Count > 0)
 | 
			
		||||
            {//Assign the default value if there are any items left in the stack
 | 
			
		||||
                Count--;
 | 
			
		||||
                value = Head!.Value;
 | 
			
		||||
                Head = Head.Next;
 | 
			
		||||
                if (Count == 0)
 | 
			
		||||
                {
 | 
			
		||||
                    Head = default;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public T? Peek()
 | 
			
		||||
        {
 | 
			
		||||
            //View item on top of the stack
 | 
			
		||||
            if (Count > 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Head!.Value;
 | 
			
		||||
            }
 | 
			
		||||
            return default;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public int GetCount()
 | 
			
		||||
        {//Return the number of items in the list
 | 
			
		||||
            return Count;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								Datastructures/Stack/StackNode.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Datastructures/Stack/StackNode.cs
									
									
									
									
									
										Normal 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
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user