// 可以通过排序解决,也可以直接倒置链表
// 下面是链表倒置代码(假定被倒置的链表没有头结点)
LinkList *Inversion(LinkList *head) {
LinkList *p = NULL,*q = head,*t;
t = q->next;
while(q) {
q->next = p;
p = q;
q = t;
t = t->next;
}
head = p;
return head;
}
#include
#include
struct qlist{
int data;
struct qlist *next;
};
struct qlist *head;
nsort()
{
struct qlist *p, *q, *t;
q = head->next;
p = q->next;
q->next = NULL;
while(p->next != NULL){
t = p->next;
head->next = p;
p->next = q;
q = p;
p = t;
}
head->next = p;
p->next = q;
}
int main()
{
int num;
struct qlist *p, *q;
head = (struct qlist *)malloc(sizeof(struct qlist));
p = head;
p->next = NULL;
while(1){
scanf("%d", &num);
if(num == 0)
break;
q = (struct qlist *)malloc(sizeof(struct qlist));
p->next = q;
p = p->next;
p->data = num;
p->next = NULL;
}
p = head->next;
while(p != NULL){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
nsort();
p = head->next;
while(p != NULL){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}