w3resource

C# Sharp Stack Exercises: Find the minimum element in a stack

C# Sharp Stack: Exercise-6 with Solution

Write a C# program to find the minimum element in a stack.

Sample Solution:

C# Code:

using System;
public class Stack
{
    private int[] items;
    private int top;

    public Stack(int size)
    {
        items = new int[size];
        top = -1;
    }

    public bool IsEmpty()
    {
        return top == -1;
    }

    public bool IsFull()
    {
        return top == items.Length - 1;
    }

    public void Push(int item)
    {
        if (IsFull())
        {
            Console.WriteLine("Stack Full!");
            return;
        }

        items[++top] = item;
    }

    public int Pop()
    {
        if (IsEmpty())
        {
            Console.WriteLine("Stack underflow");
            return -1;
        }

        return items[top--];
    }

    public int Peek()
    {
        if (IsEmpty())
        {
            Console.WriteLine("Stack is empty");
            return -1;
        }

        return items[top];
    }

    public static int Size(Stack stack)
    {
        return stack.top + 1;
    }

    // Method to get minimum value
    public static int Min(Stack stack)
    {
        if (stack.IsEmpty())
        {
            Console.WriteLine("Stack is empty");
            return -1;
        }

        // Initialize min to the first element of the stack
        int min = stack.Peek();

        // Traverse the stack and compare each element to the current min
        for (int i = stack.top; i >= 0; i--)
        {
            if (stack.items[i] < min)
            {
                min = stack.items[i];
            }
        }

        return min;
    }

    public static void Display(Stack stack)
    {
        if (stack.IsEmpty())
        {
            Console.WriteLine("Stack is empty");
            return;
        }

        Console.WriteLine("Stack elements:");
        for (int i = stack.top; i >= 0; i--)
        {
            Console.Write(stack.items[i]+" ");
        }
    }

}

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Initialize a stack:");
        Stack stack = new Stack(10);
        Stack result = new Stack(10);
        Console.WriteLine("Input some elements onto the stack:");
        stack.Push(1);
        stack.Push(3);
        stack.Push(4);
        stack.Push(9);
        stack.Push(5);
        stack.Push(6);
        Stack.Display(stack);
        int min_val = Stack.Min(stack);
        Console.WriteLine("\nMinimum element in the stack: " + min_val);
    }  
}

Sample Output:

Initialize a stack:
Input some elements onto the stack:
Stack elements:
6 5 9 4 3 1
Minimum element in the stack: 1

Flowchart:

Flowchart: Find the minimum element in a stack.
Flowchart: Find the minimum element in a stack.
Flowchart: Find the minimum element in a stack.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Find the maximum element in a stack.
Next: Remove all the elements from a stack.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.