Challenge 5: Sort Values in a Stack

In this lesson, you will learn how to sort elements using a stack.

Problem statement

You have to implement myStack sortStack(myStack stack, int size) function, which will take a stack and sort all its elements in ascending order.

Input

The input is a stack of integers.

Output

A stack with all its elements sorted is the output.

Sample input

//input stack where 23 is the top
23, 60, 12, 42, 4, 97, 2

Sample output

//resultant stack where 2 is the top
 2, 4, 12, 23, 42, 60, 97

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.