w3resource

C# Sharp Stack Exercises: Remove specified element from a stack

C# Sharp Stack: Exercise-8 with Solution

Write a C# program to remove specified element from a given 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 remove an element
    public static void remove_an_element(Stack stack, int value)
    {
        Stack temp = new Stack(Size(stack));

        while (!stack.IsEmpty())
        {
            int element = stack.Pop();

            if (element != value)
            {
                temp.Push(element);
            }
        }

        while (!temp.IsEmpty())
        {
            stack.Push(temp.Pop());
        }
    }
    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(5);
        Console.WriteLine("\nInput some elements onto the stack:");
        stack.Push(10);
        stack.Push(20);
        stack.Push(30);
        stack.Push(40);
        stack.Push(50);
        Stack.Display(stack);
        Console.WriteLine("\n\nRemove 30 from the said stack:");
        Stack.remove_an_element(stack, 30);
        Stack.Display(stack);
        Console.WriteLine("\n\nRemove 50 from the said stack:");
        Stack.remove_an_element(stack, 50);
        Stack.Display(stack);
    }
}

Sample Output:

Initialize a stack:

Input some elements onto the stack:
Stack elements:
50 40 30 20 10 

Remove 30 from the said stack:
Stack elements:
50 40 20 10 

Remove 50 from the said stack:
Stack elements:
40 20 10 

Flowchart:

Flowchart: Remove specified element from a stack.
Flowchart: Remove specified element from a stack.
Flowchart: Remove specified element from a stack.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Remove all the elements from a stack.
Next: Count all the elements in 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.