w3resource

C# Sharp Stack Exercises: Sort the elements of a stack in descending order

C# Sharp Stack: Exercise-2 with Solution

Write a C# program to sort the elements of a given stack in descending order.

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;
    }

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

        Stack tempStack = new Stack(stack.items.Length);

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

            while (!tempStack.IsEmpty() && currentItem > tempStack.Peek())
            {
                stack.Push(tempStack.Pop());
            }

            tempStack.Push(currentItem);
        }

        while (!tempStack.IsEmpty())
        {
            stack.Push(tempStack.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(10);
        Console.WriteLine("\nInput some elements onto the stack:");
        stack.Push(1);
        stack.Push(3);
        stack.Push(3);
        stack.Push(9);
        stack.Push(5);
        Stack.Display(stack);
        Stack.SortDescending(stack);
        Console.WriteLine("\nStack elements in descending order:");
        Stack.Display(stack);
        Console.WriteLine("\nInput two more elements onto the stack:");
        stack.Push(0);
        stack.Push(10);
        Stack.Display(stack);
        Console.WriteLine("\nStack elements in descending order:");
        Stack.SortDescending(stack);
        Stack.Display(stack);
    }  
}

Sample Output:

Initialize a stack:

Input some elements onto the stack:
Stack elements:
5 9 3 3 1 
Stack elements in descending order:
Stack elements:
9 5 3 3 1 
Input two more elements onto the stack:
Stack elements:
10 0 9 5 3 3 1 
Stack elements in descending order:
Stack elements:
10 9 5 3 3 1 0 

Flowchart:

Flowchart: Sort the elements of a stack in descending order.
Flowchart: Sort the elements of a stack in descending order.
Flowchart: Sort the elements of a stack in descending order.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Implement a stack with push and pop operations.
Next: Sort the elements of a stack in ascending order.

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.