#include<stack>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
stack<int> sort_stack( stack<int> st )
{
stack<int> st1;
while(!st.empty())
{
int temp = st.top();
st.pop();
while(!st1.empty() && temp>st1.top())
{
st.push(st1.top());
st1.pop();
}
st1.push(temp);
}
return st1;
}
int main()
{
int arr[] = {3,5,1,7,9,2,4,0};
stack<int> st1;
for(int i=0; i<8; i++)
st1.push(arr[i]);
stack<int> st2 = sort_stack(st1);
for(int i=0; i<8; i++)
{
printf("%d|",st2.top());
st2.pop();
}
int j=0;
scanf("%d",j);
}
一个栈,只提供push,pop,top,empty四种操作(这四种操作就是C++标准里的操作),对该栈的元素进行排序
最新推荐文章于 2024-04-29 23:41:54 发布