#pragma warning (disable:4996)
#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef int Status;
typedef struct Stack* SqList;
typedef struct Stack
{
Elemtype data;
SqList next;
}Stack;
Status InitStack(SqList& s)
{
s = (SqList)malloc(sizeof(Stack));
s->next = NULL;
return 1;
}
Status DestroyStack(SqList& s)
{
SqList p;
p = s;
while (p)
{
s = s->next;
free(p);
p = s;
}
return 1;
}
Status StackEmpty(SqList& s)
{
if (s->next == NULL)
return 1;
else
return 0;
}
Status Push(SqList& s, Elemtype e)
{
SqList p = (SqList)malloc(sizeof(Stack));
p->data = e;
p->next = s->next;
s->next = p;
return 1;
}
Status Pop(SqList& s, Elemtype& e)
{
if (s->next == NULL)
return 0;
SqList p;
p = s->next;
e = p->data;
s->next = p->next;
free(p);
return 1;
}
Status GetTop(SqList s, Elemtype& e)
{
if (s->next == NULL)
return 0;
e = s->next->data;
return 1;
}
Status Sort(SqList& s)
{
if (!s)
return 0;
Elemtype e1, e2;
SqList p = NULL;
InitStack(p);
while (!StackEmpty(s))
{
Pop(s, e1);
while (!StackEmpty(p))
{
GetTop(p, e2);
if (e2 > e1)
{
Pop(p, e2);
Push(s, e2);
}
else
break;
}
Push(p, e1);
}
while (!StackEmpty(p))
{
Pop(p, e1);
Push(s, e1);
}
DestroyStack(p);
return 1;
}
int main()
{
SqList s = NULL;
InitStack(s);
int e, i;
printf("请输入你想输入的数据个数:");
scanf("%d", &i);
for (int j = 0; j < i; j++)
{
scanf("%d", &e);
Push(s, e);
}
Sort(s);
while (!StackEmpty(s))
{
Pop(s, e);
printf("%d", e);
}
free(s);
return 0;
}