user-0 提问: 链表排序问题

回复:
user: user-1
content: 关于单链表的排序: 可以用冒泡排序法,先两个数据节点的数据域---进行比较,把大的往前或者往后排,比较n-1轮;数据就可以是从大到小排序了. 具体的交换顺序的方法就用指针存放的地址的改变实现交换位置: 但是冒泡法显然操作要比较多.

user: user-2
content: 引用 3 楼 qq_30780133 的回复:[quote=引用 2 楼 cfjtaishan 的回复:] 链表可以考虑使用插入排序方法： 以下的例子仅供参考：  #include <stdio.h> #include <stdlib.h>  struct ListNode {     int data;     struct ListNode *next; };  struct ListNode *create_list(); struct ListNode *deletem(struct ListNode *L, int m); struct ListNode *my_deletem(struct ListNode *L, int m);  void Insertsort(struct ListNode **L);  void printlist(struct ListNode *L) {     struct ListNode *p = L;     while (p) {         printf("%d ", p->data);         p = p->next;     }     printf("\n"); }  int main(void) {     int m;     struct ListNode *L = create_list();      printlist(L);     Insertsort(&L);     printlist(L);      printf("Please input your delete number: ");     scanf("%d", &m);     L = deletem(L, m);     printlist(L);      return 0; }  /* 带哨兵的链表插入排序*/ void Insertsort(struct ListNode **L) {     struct ListNode *p,*q,*r,*u;     p = (*L)->next;     (*L)->next = NULL;     while(p!=NULL)     {         r = *L;         q = (*L)->next;         while(q !=NULL && q->data <= p->data)         {             r = q;             q = q->next;         }         u=p->next;         p->next=r->next;         r->next=p;         p=u;     } }  struct ListNode *create_list() {     struct ListNode *head,*p1,*p2;     int m;     int size = sizeof(struct ListNode);      printf("Input a interge (-1 stop input)\n");     p1 = (struct ListNode*)malloc(size);     scanf("%d", &m);     p1->data = m;     head = NULL;      while (p1->data != -1) {         if(head == NULL)             head = p1;         else             p2->next = p1;         p2 = p1;         scanf("%d", &m);         p1 = (struct ListNode *)malloc(size);         p1->data = m;     }     free(p1);     p2->next = NULL;      return head; }  struct ListNode *deletem( struct ListNode *L, int m ) {     struct ListNode *ptr, *ptr1;     while( L != NULL && ( L->data == m) )     {         ptr1 = L;         L = L->next;         free(ptr1);     }     if( L == NULL )         return NULL;     ptr = L;     ptr1 = L->next;     while( ptr1 != NULL)     {         if( ptr1->data == m )         {             ptr->next = ptr1->next;             free(ptr1);         }         else             ptr = ptr1;         ptr1 = ptr->next;     }     return L; }  链表创建，输出和删除某个节点，插入排序（带哨兵的） 这个代码好像有bug哦，输入的数中有0的话，0的顺序是错的[/quote] 恩，输入的第一个应该是哨兵。 建议只看带哨兵的插入排序吧，其他函数，比如链表创建，链表删除和链表输出都是针对不带哨兵的情况，但是这个排序是针对带哨兵的。

user: user-0
content: 引用 2 楼 cfjtaishan 的回复:链表可以考虑使用插入排序方法： 以下的例子仅供参考：  #include <stdio.h> #include <stdlib.h>  struct ListNode {     int data;     struct ListNode *next; };  struct ListNode *create_list(); struct ListNode *deletem(struct ListNode *L, int m); struct ListNode *my_deletem(struct ListNode *L, int m);  void Insertsort(struct ListNode **L);  void printlist(struct ListNode *L) {     struct ListNode *p = L;     while (p) {         printf("%d ", p->data);         p = p->next;     }     printf("\n"); }  int main(void) {     int m;     struct ListNode *L = create_list();      printlist(L);     Insertsort(&L);     printlist(L);      printf("Please input your delete number: ");     scanf("%d", &m);     L = deletem(L, m);     printlist(L);      return 0; }  /* 带哨兵的链表插入排序*/ void Insertsort(struct ListNode **L) {     struct ListNode *p,*q,*r,*u;     p = (*L)->next;     (*L)->next = NULL;     while(p!=NULL)     {         r = *L;         q = (*L)->next;         while(q !=NULL && q->data <= p->data)         {             r = q;             q = q->next;         }         u=p->next;         p->next=r->next;         r->next=p;         p=u;     } }  struct ListNode *create_list() {     struct ListNode *head,*p1,*p2;     int m;     int size = sizeof(struct ListNode);      printf("Input a interge (-1 stop input)\n");     p1 = (struct ListNode*)malloc(size);     scanf("%d", &m);     p1->data = m;     head = NULL;      while (p1->data != -1) {         if(head == NULL)             head = p1;         else             p2->next = p1;         p2 = p1;         scanf("%d", &m);         p1 = (struct ListNode *)malloc(size);         p1->data = m;     }     free(p1);     p2->next = NULL;      return head; }  struct ListNode *deletem( struct ListNode *L, int m ) {     struct ListNode *ptr, *ptr1;     while( L != NULL && ( L->data == m) )     {         ptr1 = L;         L = L->next;         free(ptr1);     }     if( L == NULL )         return NULL;     ptr = L;     ptr1 = L->next;     while( ptr1 != NULL)     {         if( ptr1->data == m )         {             ptr->next = ptr1->next;             free(ptr1);         }         else             ptr = ptr1;         ptr1 = ptr->next;     }     return L; }  链表创建，输出和删除某个节点，插入排序（带哨兵的） 这个代码好像有bug哦，输入的数中有0的话，0的顺序是错的

user: user-2
content: 链表可以考虑使用插入排序方法： 以下的例子仅供参考：  #include <stdio.h> #include <stdlib.h>  struct ListNode {     int data;     struct ListNode *next; };  struct ListNode *create_list(); struct ListNode *deletem(struct ListNode *L, int m); struct ListNode *my_deletem(struct ListNode *L, int m);  void Insertsort(struct ListNode **L);  void printlist(struct ListNode *L) {     struct ListNode *p = L;     while (p) {         printf("%d ", p->data);         p = p->next;     }     printf("\n"); }  int main(void) {     int m;     struct ListNode *L = create_list();      printlist(L);     Insertsort(&L);     printlist(L);      printf("Please input your delete number: ");     scanf("%d", &m);     L = deletem(L, m);     printlist(L);      return 0; }  /* 带哨兵的链表插入排序*/ void Insertsort(struct ListNode **L) {     struct ListNode *p,*q,*r,*u;     p = (*L)->next;     (*L)->next = NULL;     while(p!=NULL)     {         r = *L;         q = (*L)->next;         while(q !=NULL && q->data <= p->data)         {             r = q;             q = q->next;         }         u=p->next;         p->next=r->next;         r->next=p;         p=u;     } }  struct ListNode *create_list() {     struct ListNode *head,*p1,*p2;     int m;     int size = sizeof(struct ListNode);      printf("Input a interge (-1 stop input)\n");     p1 = (struct ListNode*)malloc(size);     scanf("%d", &m);     p1->data = m;     head = NULL;      while (p1->data != -1) {         if(head == NULL)             head = p1;         else             p2->next = p1;         p2 = p1;         scanf("%d", &m);         p1 = (struct ListNode *)malloc(size);         p1->data = m;     }     free(p1);     p2->next = NULL;      return head; }  struct ListNode *deletem( struct ListNode *L, int m ) {     struct ListNode *ptr, *ptr1;     while( L != NULL && ( L->data == m) )     {         ptr1 = L;         L = L->next;         free(ptr1);     }     if( L == NULL )         return NULL;     ptr = L;     ptr1 = L->next;     while( ptr1 != NULL)     {         if( ptr1->data == m )         {             ptr->next = ptr1->next;             free(ptr1);         }         else             ptr = ptr1;         ptr1 = ptr->next;     }     return L; }  链表创建，输出和删除某个节点，插入排序（带哨兵的）

user: user-3
content: 将两个有序链表合并成一个有序链表 http://blog.csdn.net/lalor/article/details/7429989