Sort the Stack Array by R4R Team

following is the fuction which are used to sort the stack:

stack< int> sortStack(stack< int> &input)
{
stack< int> tmpStack;

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 (!tmpStack.empty() && tmpStack.top() > tmp)
{
// pop from temporary stack and push
// it to the input stack
input.push(tmpStack.top());
tmpStack.pop();
}

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

return tmpStack;
}

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!