Click here to Skip to main content
15,887,988 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
// C++ program to sort a stack using an 
// auxiliary stack. 
#include <bits/stdc++.h> 
using namespace std; 
  
// This function return the sorted 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; 
} 
  
// main function 
int main() 
{ 
    stack<int> input; 
    input.push(34); 
    input.push(3); 
    input.push(31); 
    input.push(98); 
    input.push(92); 
    input.push(23); 
  
    // This is the temporary stack 
    stack<int> tmpStack = sortStack(input); 
    cout << "Sorted numbers are:\n"; 
  
    while (!tmpStack.empty()) 
    { 
        cout << tmpStack.top()<< " "; 
        tmpStack.pop(); 
    } 
} 


What I have tried:

conver C progrramıng pls , sort list integer decreasing
Posted
Updated 3-Dec-20 20:12pm
v2
Comments
Rick York 4-Dec-20 12:16pm    
The word is please.

The major difficult in your task is implementing the stack in C (as oppose to using the C++ standard library existing one).
However, I guess you could find many many examples on the web, just performing a bit of Googling...[^]
 
Share this answer
 
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And frankly? You'd learn nothing from this if we did, and the whole point of giving you homework is for you to learn how to do this for yourself.
 
Share this answer
 
Comments
Emre Yaşar 4-Dec-20 2:14am    
ı need convert C
OriginalGriff 4-Dec-20 2:25am    
No, you don't.
The reason why is simple: C does not support the Stack class (or indeed any classes at all) that C++ does - so all the work that the class does for you would have to be implemented for you, and that's the whole point of your homework!
You are supposed to implement a stack for yourself so you learn something from the exercise. Writing a "C usable" version of a class based C++ solution will produce dreadful C code, and you'll get the failing grade you deserve anyway ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900