建立一个升序链表并遍历输出
struct LNode{
int data;
LNode* next;
};
void Insert(LNode* List, int data)
{
LNode* tmp=new LNode;
tmp->data = data;
tmp->next = NULL;
LNode* pre, *now;
now = List->next;
pre = List;
while (now!=NULL&&now->data<data)
{
pre = now;
now = now->next;
}
tmp->next = now;
pre->next = tmp;
}
void slove()
{
int x,n;
LNode * head = new LNode;
while (~scanf("%d",&n))
{
head->next = NULL;
head->data = 0;
for (int i = 0; i < n; ++i)
{
scanf("%d", &x);
Insert(head, x);
}
LNode *now = head->next;
while (now)
{
if (now->next == NULL)
printf("%d", now->data);
else
printf("%d ", now->data);
now = now->next;
}
printf("\n");
}
}