user-0 提问: 链表删除操作遇到Segmentation fault，求指点

回复:
user: user-0
content: 引用 1 楼 cfjtaishan 的回复:#include<stdio.h> #include<stdlib.h> #include<string.h>  #define LEN sizeof(struct Clerk)  struct Clerk {     long num;     char name[30];     struct Clerk *next; }; int n;  struct Clerk *creat(void) {     struct Clerk *head = NULL;     struct Clerk *p1 = NULL;     struct Clerk *p2 = NULL;     n = 0;      p1 = p2 = (struct Clerk *) malloc(LEN);     printf("please enter num and name:\n");     scanf("%ld,%s", &p1->num, p1->name);     head = NULL;     while(0 != strncmp("end", p1->name, 3))     {         n = n + 1;         if(1 == n){             head = p1;         }         else{             p2->next = p1;         }         p2 = p1;         p1 = (struct Clerk *)malloc(LEN);         scanf("%ld,%s", &p1->num, p1->name);     }     free(p1);     p2->next = NULL;     return(head); }  void print(struct Clerk *head) {     struct Clerk *p = NULL;     printf("this %d clerks are:\n", n);     p = head;     if(head != NULL) {    /*here del ';' */         do {             printf("%ld  %s\n", p->num, p->name);             p = p->next;         }while(p != NULL);     } }  struct Clerk *del(struct Clerk *head,long num) {     struct Clerk *p1 = NULL;     struct Clerk *p2 = NULL;      if(head == NULL){    /*here, not '=' */         return head;     }     p1 = head;     while(num != p1->num && p1->next != NULL)     {         p2 = p1;         p1 = p1->next;     }     if(num == p1->num){         if(p1 == head){             head = p1->next;         }         else{             p2->next = p1->next;         }         n = n - 1;     }     else{     }      return head; }   struct Clerk *insert(struct Clerk *head, struct Clerk *cl) {     struct Clerk *p0 = NULL;     struct Clerk *p1 = NULL;     struct Clerk *p2 = NULL;     p1 = head;     p0 = cl;      if(head == NULL){         head = p0;     }     else{         while((p0->num > p1->num) && (p1-> next != NULL))         {             p2 = p1;             p1 = p1->next;         }         if(p0->num <= p1->num){             if(head == p1){                 head = p0;                 p0->next = p1;             }             else{                 p2->next = p0;                 p0->next = NULL;             }         }         n = n + 1;     }         return head; }  int main(int argc, const char *argv[]) {     struct Clerk *head,cl;     long del_num;      head = creat();     print(head);      printf("input the deleted number:\n");     scanf("%ld", &del_num);     head = del(head, del_num);     print(head);      printf("input clerk:\n");     scanf("%ld,%s", &cl.num, cl.name);     head = insert(head, &cl);     print(head);      return 0; } 有几处逻辑错误；详见上面代码里的注释。 对比一下代码，然后测试一下看看是否还有问题。 解决了，唉！。。。。总是犯这种错误，还找不到。。。多谢

user: user-1
content: 引用 3 楼 hekylej 的回复:[quote=引用 2 楼 cfjtaishan 的回复:] 另外，del函数里，return head放在else外面。 return head是在else外面的[/quote] 看错了，是insert函数

user: user-0
content: 引用 2 楼 cfjtaishan 的回复:另外，del函数里，return head放在else外面。 return head是在else外面的

user: user-1
content: 另外，del函数里，return head放在else外面。

user: user-1
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  #define LEN sizeof(struct Clerk)  struct Clerk {     long num;     char name[30];     struct Clerk *next; }; int n;  struct Clerk *creat(void) {     struct Clerk *head = NULL;     struct Clerk *p1 = NULL;     struct Clerk *p2 = NULL;     n = 0;      p1 = p2 = (struct Clerk *) malloc(LEN);     printf("please enter num and name:\n");     scanf("%ld,%s", &p1->num, p1->name);     head = NULL;     while(0 != strncmp("end", p1->name, 3))     {         n = n + 1;         if(1 == n){             head = p1;         }         else{             p2->next = p1;         }         p2 = p1;         p1 = (struct Clerk *)malloc(LEN);         scanf("%ld,%s", &p1->num, p1->name);     }     free(p1);     p2->next = NULL;     return(head); }  void print(struct Clerk *head) {     struct Clerk *p = NULL;     printf("this %d clerks are:\n", n);     p = head;     if(head != NULL) {    /*here del ';' */         do {             printf("%ld  %s\n", p->num, p->name);             p = p->next;         }while(p != NULL);     } }  struct Clerk *del(struct Clerk *head,long num) {     struct Clerk *p1 = NULL;     struct Clerk *p2 = NULL;      if(head == NULL){    /*here, not '=' */         return head;     }     p1 = head;     while(num != p1->num && p1->next != NULL)     {         p2 = p1;         p1 = p1->next;     }     if(num == p1->num){         if(p1 == head){             head = p1->next;         }         else{             p2->next = p1->next;         }         n = n - 1;     }     else{     }      return head; }   struct Clerk *insert(struct Clerk *head, struct Clerk *cl) {     struct Clerk *p0 = NULL;     struct Clerk *p1 = NULL;     struct Clerk *p2 = NULL;     p1 = head;     p0 = cl;      if(head == NULL){         head = p0;     }     else{         while((p0->num > p1->num) && (p1-> next != NULL))         {             p2 = p1;             p1 = p1->next;         }         if(p0->num <= p1->num){             if(head == p1){                 head = p0;                 p0->next = p1;             }             else{                 p2->next = p0;                 p0->next = NULL;             }         }         n = n + 1;     }         return head; }  int main(int argc, const char *argv[]) {     struct Clerk *head,cl;     long del_num;      head = creat();     print(head);      printf("input the deleted number:\n");     scanf("%ld", &del_num);     head = del(head, del_num);     print(head);      printf("input clerk:\n");     scanf("%ld,%s", &cl.num, cl.name);     head = insert(head, &cl);     print(head);      return 0; } 有几处逻辑错误；详见上面代码里的注释。 对比一下代码，然后测试一下看看是否还有问题。