Stack implementation in c#

Jack Herer 105 Reputation points
2023-04-20T16:39:03.6866667+00:00

I'm just posting how to implement stack in c#... I hope someone finds it helpfull

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack Herer 105 Reputation points
    2023-04-20T16:43:23.2966667+00:00
    Console.WriteLine("Hello, World!");
    Stack stack = new();
    stack.Push(4);
    stack.Push(2);
    stack.Push(0);
    stack.Push(420);
    stack.Push(69);
    stack.Push(42069);
    stack.Peek();
    Console.WriteLine($"Item popped: {stack.Pop()}");
    Console.WriteLine();
    stack.PrintAll();
    
    
    internal class Stack
    {
        // length of array
        static readonly int max = 1000;
        int top;
        // array of ints of specified length
        int[] stack = new int[max];
    
        public Stack()
        {
            this.top = -1;
        }
    
        bool isEmpty()
        {
            return (top < 0);
        }
    
        // method for adding data
        internal bool Push(int data)
        {
            // if is arrray already full, returns false, else sets new value as top value
            if (this.top >= max)
            {
                Console.WriteLine("Stack Overflow");
                return false;
            }
            else
            {
                stack[++this.top] = data;
                return true;
            }
        }
    
        // method to remove last element in stack
        internal int? Pop()
        {
            // if is stack empty, returns nothing, else returns last value
            if (this.isEmpty())
            {
                Console.WriteLine("Stack Underflow");
                return null;
            }
            else
            {
                int value = stack[this.top--];
                return value;
            }
        }
    
        // method to return last element in stack
        internal void Peek()
        {
            // if is stack empty, returns nothing, else returns last value
            if (this.isEmpty())
            {
                Console.WriteLine("Stack Underflow");
                return;
            }
            else
            {
                Console.WriteLine($"The topmost element of stack is {stack[top]}");
            }
        }
        // method to print all elements in stack
        internal void PrintAll()
        {
            if (this.isEmpty())
            {
                Console.WriteLine("The array is empty");
                return;
            }
            else
            {
                Console.WriteLine("Items of stack are: ");
                for (int i = top; i >= 0; i--)
                {
                    Console.WriteLine(stack[i]);
                }
            }
        }
    }
    
    0 comments No comments

  2. Bruce (SqlWork.com) 61,731 Reputation points
    2023-04-20T17:10:20.5566667+00:00

    .net already implemented a generic stack.

    https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1?view=net-7.0.

    Also it dynamically resizes the backing array so it not limited in size. For learning you might refactor to generic and handle size allocations.

    dotnet reference source:

    https://github.com/microsoft/referencesource/blob/master/System/compmod/system/collections/generic/stack.cs

    dotnet core source: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs

    0 comments No comments