#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
Node *first;
void List()
{
first = (struct Node *)malloc(sizeof(struct Node));
first->next = first;
}
void Insert(int val)
{
Node *n = (struct Node *)malloc(sizeof(struct Node));
n->data = val;
Node *temp=first;
while (temp->next!=first)
temp = temp->next;
n->next = temp->next;
temp->next = n;
}
// 删除并打印
void DeleteAndPrint()
{
int min;
Node *Head=first;//用Head标注开始位置
while (first->next!=first)//看是不是就还剩一个元素
{
Node *pre=first,*p=first->next,*premin=pre,*min=p;
while(p!=Head)//判断循环是否已经完全遍历链表
{
if(min->data>p->data)
{
min=p;
premin=pre;//记住最小元素所在位置的前驱结点
}
pre=p;
p=p->next;
}
Node *temp=min;//标注最小结点
printf("%d ",min->data);
premin->next=min->next;
free(temp);//释放节点空间
}
free(first);// write your code here
}
int main()
{
List();
int val;
scanf("%d", &val);
while (val!=-1)
{
Insert(val);
scanf("%d", &val);
}
DeleteAndPrint();
return 0;
}