#include "SeqStack.h"
SeqStack YouXuZhan(SeqStack *s)
{
SeqStack t;
StackInitiate(&t);
int x;
int y;
while (StackNotEmpty(*s))
{
StackPop(s, &x);
while (StackNotEmpty(t) && StackTop(t)>x)
{
StackPush(s, StackTop(t));
StackPop(&t, &y);
}
StackPush(&t, x);
}
return t;
}
int main()
{
SeqStack s;
int x;
StackInitiate(&s);
StackPush(&s, 1);
StackPush(&s, 4);
StackPush(&s, 3);
StackPush(&s, 2);
s=YouXuZhan(&s);
printf("排序后:
");
while (StackNotEmpty(s))
{
StackPop(&s, &x);
printf("%d ", x);
}
return 0;
}
排序后:
4 3 2 1
(出栈后与原栈序相反)