user-0 提问: 链表删除问题，与常规删除节点不同

回复:
user: user-0
content: 引用 2 楼 cfjtaishan 的回复:void delet(int disnum, n *head) {     n p, q;      p = q = *head;     while (p) {         if (p->data == disnum) {             if (p == *head) {                 *head = (*head)->next;                 free(p);                 p = q = *head;                 continue;             } else {                 q->next = p->next;                 free(p);                 p = q;                 p = p->next;                 continue;             }         }         q = p;         p = p->next;     } } 试一下这个版本。  以下是我的测试代码：  #include <stdio.h> #include <stdlib.h>  typedef struct node {     int data;     struct node *next; }*n;   n create_link(int num) {     int i;     n p, q, phead = NULL;      for (i = 0; i < num; i++) {         p = (n)malloc(sizeof (struct node));         if (!p) {             fprintf(stderr, "malloc error!\n");             exit(-1);         }         scanf("%d", &p->data);          if (!phead) {             phead = p;             q = p;         } else {             q->next = p;             q = p;         }     }     q->next = NULL;      return phead; }  void print_link(n phead) {     n p = phead;      while (p) {         printf("%d\t", p->data);         p = p->next;     }     putchar(10); }  void delet(int disnum, n *head) {     n p, q;      p = q = *head;     while (p) {         if (p->data == disnum) {             if (p == *head) {                 *head = (*head)->next;                 free(p);                 p = q = *head;                 continue;             } else {                 q->next = p->next;                 free(p);                 p = q;                 p = p->next;                 continue;             }         }         q = p;         p = p->next;     } }  int main(void) {     int num;     n head;      scanf("%d", &num);     head = create_link(num);     print_link(head);      scanf("%d", &num);     delet(num, &head);     print_link(head);     return 0; }  你好棒棒啊，我去思考下你代码了，

user: user-0
content: 引用 2 楼 cfjtaishan 的回复:void delet(int disnum, n *head) {     n p, q;      p = q = *head;     while (p) {         if (p->data == disnum) {             if (p == *head) {                 *head = (*head)->next;                 free(p);                 p = q = *head;                 continue;             } else {                 q->next = p->next;                 free(p);                 p = q;                 p = p->next;                 continue;             }         }         q = p;         p = p->next;     } } 试一下这个版本。  以下是我的测试代码：  #include <stdio.h> #include <stdlib.h>  typedef struct node {     int data;     struct node *next; }*n;   n create_link(int num) {     int i;     n p, q, phead = NULL;      for (i = 0; i < num; i++) {         p = (n)malloc(sizeof (struct node));         if (!p) {             fprintf(stderr, "malloc error!\n");             exit(-1);         }         scanf("%d", &p->data);          if (!phead) {             phead = p;             q = p;         } else {             q->next = p;             q = p;         }     }     q->next = NULL;      return phead; }  void print_link(n phead) {     n p = phead;      while (p) {         printf("%d\t", p->data);         p = p->next;     }     putchar(10); }  void delet(int disnum, n *head) {     n p, q;      p = q = *head;     while (p) {         if (p->data == disnum) {             if (p == *head) {                 *head = (*head)->next;                 free(p);                 p = q = *head;                 continue;             } else {                 q->next = p->next;                 free(p);                 p = q;                 p = p->next;                 continue;             }         }         q = p;         p = p->next;     } }  int main(void) {     int num;     n head;      scanf("%d", &num);     head = create_link(num);     print_link(head);      scanf("%d", &num);     delet(num, &head);     print_link(head);     return 0; }  谢谢

user: user-1
content: void delet(int disnum, n *head) {     n p, q;      p = q = *head;     while (p) {         if (p->data == disnum) {             if (p == *head) {                 *head = (*head)->next;                 free(p);                 p = q = *head;                 continue;             } else {                 q->next = p->next;                 free(p);                 p = q;                 p = p->next;                 continue;             }         }         q = p;         p = p->next;     } } 试一下这个版本。  以下是我的测试代码：  #include <stdio.h> #include <stdlib.h>  typedef struct node {     int data;     struct node *next; }*n;   n create_link(int num) {     int i;     n p, q, phead = NULL;      for (i = 0; i < num; i++) {         p = (n)malloc(sizeof (struct node));         if (!p) {             fprintf(stderr, "malloc error!\n");             exit(-1);         }         scanf("%d", &p->data);          if (!phead) {             phead = p;             q = p;         } else {             q->next = p;             q = p;         }     }     q->next = NULL;      return phead; }  void print_link(n phead) {     n p = phead;      while (p) {         printf("%d\t", p->data);         p = p->next;     }     putchar(10); }  void delet(int disnum, n *head) {     n p, q;      p = q = *head;     while (p) {         if (p->data == disnum) {             if (p == *head) {                 *head = (*head)->next;                 free(p);                 p = q = *head;                 continue;             } else {                 q->next = p->next;                 free(p);                 p = q;                 p = p->next;                 continue;             }         }         q = p;         p = p->next;     } }  int main(void) {     int num;     n head;      scanf("%d", &num);     head = create_link(num);     print_link(head);      scanf("%d", &num);     delet(num, &head);     print_link(head);     return 0; }

user: user-2
content: 数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633