This C++ program demonstrates how to sort a stack using only another temporary stack. The program sorts the original stack in ascending order where the smallest elements end up on the top of the stack.

Program Code

#include <iostream>
#include <stack>

/**
 * Sorts a stack using another stack.
 * @param input Stack to be sorted.
 * @return A sorted stack where the smallest items are on top.
 */
std::stack<int> sortStack(std::stack<int>& input) {
    std::stack<int> tempStack;

    while (!input.empty()) {
        // Pop out the first element
        int tmp = input.top();
        input.pop();

        // While temporary stack is not empty and top
        // of stack is greater than temp
        while (!tempStack.empty() && tempStack.top() > tmp) {
            // pop from temporary stack and push
            // it to the input stack
            input.push(tempStack.top());
            tempStack.pop();
        }

        // push temp in tempory of stack
        tempStack.push(tmp);
    }

    return tempStack;
}

int main() {
    std::stack<int> input;
    input.push(34);
    input.push(3);
    input.push(31);
    input.push(98);
    input.push(92);
    input.push(23);

    // Function call to sort the stack
    std::stack<int> sortedStack = sortStack(input);

    std::cout << "Sorted numbers are:\n";

    while (!sortedStack.empty()) {
        std::cout << sortedStack.top() << " ";
        sortedStack.pop();
    }

    return 0;
}

Function Documentation

std::stack<int> sortStack(std::stack<int>& input)

This function sorts an input stack using another stack and returns the sorted stack.

  • Parameters:
    • input: Reference to the stack that needs to be sorted.
  • Returns: A sorted stack with the smallest items on top.
  • Description:The function utilizes a temporary stack to hold elements in sorted order. Elements from the input stack are popped and compared with the elements of the temporary stack. If the current element from the input stack is smaller than the top of the temporary stack, elements from the temporary stack are moved back to the input stack until the correct position for the current element is found in the temporary stack.

Example Usage

        std::stack<int> input;
        input.push(34);
        input.push(3);
        input.push(31);
        input.push(98);
        input.push(92);
        input.push(23);

        std::stack<int> sortedStack = sortStack(input);

        while (!sortedStack.empty()) {
            std::cout << sortedStack.top() << " ";
            sortedStack.pop();
        }

This will output: 3 23 31 34 92 98

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)