w3resource

C# Sharp Stack Exercises: Count specified element in a stack

C# Sharp Stack: Exercise-10 with Solution

Write a C# program to count specified element in 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 count specified elements in a stack
    public static int Count(Stack stack, int value)
    {
        int count = 0;

        Stack temp = new Stack(Size(stack));

        while (!stack.IsEmpty())
        {
            int current = stack.Pop();
            if (current == value)
            {
                count++;
            }
            temp.Push(current);
        }

        while (!temp.IsEmpty())
        {
            stack.Push(temp.Pop());
        }

        return count;
    }
    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);
        Console.WriteLine("Input some elements onto the stack:");
        stack.Push(10);
        stack.Push(50);
        stack.Push(30);
        stack.Push(40);
        stack.Push(50);
        stack.Push(50);
        Stack.Display(stack);
        int value = 50;
        int count = Stack.Count(stack, value);
        Console.WriteLine("\nNumber of occurrences of {0} in stack: {1}", value, count);
    }
}

Sample Output:

Initialize a stack:
Input some elements onto the stack:
Stack elements:
50 50 40 30 50 10 
Number of occurrences of 50 in stack: 3

Flowchart:

Flowchart: Count specified element in a stack.
Flowchart: Count specified element in a stack.
Flowchart: Count specified element in a stack.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Count all the elements in a stack.
Next: Check if an element is present or not 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.