user-0 提问: [求助求助]删除链表为什么会失败

回复:
user: user-0
content: 引用 13 楼 自信男孩的回复:不明白你的意思，你要实现成什么样子的？ 刚刚写查找链表出了问题然后加上了&&就能够运行了因为我只写了while(strcmp(头结点->x，a)!=0)

user: user-0
content: 引用 13 楼 自信男孩的回复:不明白你的意思，你要实现成什么样子的？ 基本都懂了就是为什么while(Freelist && strcmp(freelist->x, a) != 0)为什么要&&freelist

user: user-0
content: 引用 1 楼 自信男孩的回复:#define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <malloc.h> # include <string.h>  struct studant {     char x[50];     struct studant* next; };  //创建链表 <2>   靠插入来完成链表 struct studant* Apex() {     struct studant* AApex = (struct studant*)malloc(sizeof(struct studant));     AApex->next = NULL;      return AApex; }  //创建链表 <1>    先制作链表再来操作链表 struct studant* apex(void) {     int i, g;     struct studant* papex;     papex = (struct studant*)malloc(sizeof(struct studant));     papex->next = NULL;      struct studant* aapex = papex;     aapex->next = NULL;      printf("需要多少个链表：");     scanf("%d", &g);     while (getchar() != '\n');     for (i = 0; i < g; i++)     {         struct studant* paapex = (struct studant*)malloc(sizeof(struct studant));         paapex->next = NULL;         printf("输入第%d个链表内的数据：", i + 1);         scanf("%s", paapex->x);         while (getchar() != '\n');         paapex->next = aapex->next;         aapex->next = paapex;     }     return papex; }  //插入节点 struct studant* zjlist(struct studant* ppapex, char a[50]) {     struct studant* qqapex;     qqapex = (struct studant*)malloc(sizeof(struct studant));     strcpy(qqapex->x, a);     qqapex->next = ppapex->next;     ppapex->next = qqapex;     return qqapex; }  //函数 头插法插入链表（增） void sclist(struct studant* llist) {     char z;     char zz[20];     int i = 1;     printf("是否开始(y/n):");     scanf("%c", &z);     while (getchar() != '\n');     if (z == 'y' || z == 'Y')         while (z == 'y' || z == 'Y')         {             printf("\n链表%d\n", i++);             printf("输入数据：");             //scanf("%s", &zz);             scanf("%s", zz);             while (getchar() != '\n');             zjlist(llist, zz);             printf("\n是否继续(y/n)：");             scanf("%c", &z);             while (getchar() != '\n')                 ;             //if (z == 'y' || z == 'Y');   //判断是否继续创建插入链表             if (z != 'y' || z != 'Y')    //判断是否继续创建插入链表                 break;         } }  //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     if (Freelist->next == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     while (Freelist->x != a)     {         SFreelist = Freelist;         Freelist = Freelist->next;         if (Freelist->next == NULL)         {             break;         }     }     if (Freelist->x == a)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");     }     else         printf("没有相关数据！无法删除！！\n"); }  //打印遍历链表 void bllist(struct studant* bllist) {     struct studant* bblist = bllist->next;//让指针指向头结点的地址然后从下一个节点开始打印（因为头结点不存放数据  会乱码  ）     if (bblist != NULL)         while (bblist != NULL)         {             printf("%s\n", bblist->x);             bblist = bblist->next;         }     else         printf("链表为空！\n"); }  int main(void) {     char a;     char v[50];     struct studant* list = Apex(); //apex();     bllist(list);//遍历链表     //     printf("要增加链表吗(y/n)：");     scanf("%c", &a);     while (getchar() != '\n')         ;     if (a == 'y' || a == 'Y')     {         sclist(list);         bllist(list);     }     //     printf("\n要删除链表吗(y/n)：");     scanf("%c", &a);     while (getchar() != '\n');     if (a == 'y' || a == 'Y')     {         printf("输入要删除的数据：");         //scanf("%s", &v);         scanf("%s", v);         shanlist(list, v);     }     //     printf("是否要再次遍历(y/n)：");     scanf("%c", &a);     printf("\n");     while (getchar() != '\n');     if (a == 'y' || a == 'Y')     {         bllist(list);     }      return 0; } 供参考～ 好像一样啊怎么参考

user: user-1
content: #define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <malloc.h> # include <string.h>  struct studant {     char x[50];     struct studant* next; };  //创建链表 <2>   靠插入来完成链表 struct studant* Apex() {     struct studant* AApex = (struct studant*)malloc(sizeof(struct studant));     AApex->next = NULL;      return AApex; }  //创建链表 <1>    先制作链表再来操作链表 struct studant* apex(void) {     int i, g;     struct studant* papex;     papex = (struct studant*)malloc(sizeof(struct studant));     papex->next = NULL;      struct studant* aapex = papex;     aapex->next = NULL;      printf("需要多少个链表：");     scanf("%d", &g);     while (getchar() != '\n');     for (i = 0; i < g; i++)     {         struct studant* paapex = (struct studant*)malloc(sizeof(struct studant));         paapex->next = NULL;         printf("输入第%d个链表内的数据：", i + 1);         scanf("%s", paapex->x);         while (getchar() != '\n');         paapex->next = aapex->next;         aapex->next = paapex;     }     return papex; }  //插入节点 struct studant* zjlist(struct studant* ppapex, char a[50]) {     struct studant* qqapex;     qqapex = (struct studant*)malloc(sizeof(struct studant));     strcpy(qqapex->x, a);     qqapex->next = ppapex->next;     ppapex->next = qqapex;     return qqapex; }  //函数 头插法插入链表（增） void sclist(struct studant* llist) {     char z;     char zz[20];     int i = 1;     printf("是否开始(y/n):");     scanf("%c", &z);     while (getchar() != '\n');     if (z == 'y' || z == 'Y')         while (z == 'y' || z == 'Y')         {             printf("\n链表%d\n", i++);             printf("输入数据：");             //scanf("%s", &zz);             scanf("%s", zz);             while (getchar() != '\n');             zjlist(llist, zz);             printf("\n是否继续(y/n)：");             scanf("%c", &z);             while (getchar() != '\n')                 ;             //if (z == 'y' || z == 'Y');   //判断是否继续创建插入链表             if (z != 'y' || z != 'Y')    //判断是否继续创建插入链表                 break;         } }  //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     if (Freelist->next == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     while (Freelist->x != a)     {         SFreelist = Freelist;         Freelist = Freelist->next;         if (Freelist->next == NULL)         {             break;         }     }     if (Freelist->x == a)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");     }     else         printf("没有相关数据！无法删除！！\n"); }  //打印遍历链表 void bllist(struct studant* bllist) {     struct studant* bblist = bllist->next;//让指针指向头结点的地址然后从下一个节点开始打印（因为头结点不存放数据  会乱码  ）     if (bblist != NULL)         while (bblist != NULL)         {             printf("%s\n", bblist->x);             bblist = bblist->next;         }     else         printf("链表为空！\n"); }  int main(void) {     char a;     char v[50];     struct studant* list = Apex(); //apex();     bllist(list);//遍历链表     //     printf("要增加链表吗(y/n)：");     scanf("%c", &a);     while (getchar() != '\n')         ;     if (a == 'y' || a == 'Y')     {         sclist(list);         bllist(list);     }     //     printf("\n要删除链表吗(y/n)：");     scanf("%c", &a);     while (getchar() != '\n');     if (a == 'y' || a == 'Y')     {         printf("输入要删除的数据：");         //scanf("%s", &v);         scanf("%s", v);         shanlist(list, v);     }     //     printf("是否要再次遍历(y/n)：");     scanf("%c", &a);     printf("\n");     while (getchar() != '\n');     if (a == 'y' || a == 'Y')     {         bllist(list);     }      return 0; } 供参考～

user: user-1
content: 不明白你的意思，你要实现成什么样子的？

user: user-0
content: 引用 9 楼 自信男孩 的回复:[quote=引用 8 楼 int i； 的回复:] [quote=引用 7 楼 自信男孩 的回复:] //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     //if (Freelist->next == NULL)     if (Freelist == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     //while (Freelist->x != a)     while (Freelist && strcmp(Freelist->x, a) != 0)     {         SFreelist = Freelist;         Freelist = Freelist->next;         /*         if (Freelist->next == NULL)         {             break;         }         */     }     if (strcmp(Freelist->x, a) == 0)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");         return;     }     //else         printf("没有相关数据！无法删除！！\n"); }  供参考～  楼主的程序只能创建一个节点和删除一个节点。这个比较好就是这个改成这样好像会跳过[/quote]  就是只能输入y/Y才能继续呢。[/quote]  注释又不行了

user: user-0
content: 引用 9 楼 自信男孩 的回复:[quote=引用 8 楼 int i； 的回复:] [quote=引用 7 楼 自信男孩 的回复:] //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     //if (Freelist->next == NULL)     if (Freelist == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     //while (Freelist->x != a)     while (Freelist && strcmp(Freelist->x, a) != 0)     {         SFreelist = Freelist;         Freelist = Freelist->next;         /*         if (Freelist->next == NULL)         {             break;         }         */     }     if (strcmp(Freelist->x, a) == 0)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");         return;     }     //else         printf("没有相关数据！无法删除！！\n"); }  供参考～  楼主的程序只能创建一个节点和删除一个节点。这个比较好就是这个改成这样好像会跳过[/quote]  就是只能输入y/Y才能继续呢。[/quote] 为什么在前面单独使用函数后后面if才能打印呢

user: user-0
content: 引用 7 楼 自信男孩 的回复://删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     //if (Freelist->next == NULL)     if (Freelist == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     //while (Freelist->x != a)     while (Freelist && strcmp(Freelist->x, a) != 0)     {         SFreelist = Freelist;         Freelist = Freelist->next;         /*         if (Freelist->next == NULL)         {             break;         }         */     }     if (strcmp(Freelist->x, a) == 0)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");         return;     }     //else         printf("没有相关数据！无法删除！！\n"); }  供参考～  楼主的程序只能创建一个节点和删除一个节点。果然是大佬  就是有时候最后输入遍历不出来

user: user-1
content: 引用 8 楼 int i； 的回复:[quote=引用 7 楼 自信男孩 的回复:] //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     //if (Freelist->next == NULL)     if (Freelist == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     //while (Freelist->x != a)     while (Freelist && strcmp(Freelist->x, a) != 0)     {         SFreelist = Freelist;         Freelist = Freelist->next;         /*         if (Freelist->next == NULL)         {             break;         }         */     }     if (strcmp(Freelist->x, a) == 0)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");         return;     }     //else         printf("没有相关数据！无法删除！！\n"); }  供参考～  楼主的程序只能创建一个节点和删除一个节点。这个比较好就是这个改成这样好像会跳过[/quote]  就是只能输入y/Y才能继续呢。

user: user-0
content: 引用 7 楼 自信男孩 的回复://删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     //if (Freelist->next == NULL)     if (Freelist == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     //while (Freelist->x != a)     while (Freelist && strcmp(Freelist->x, a) != 0)     {         SFreelist = Freelist;         Freelist = Freelist->next;         /*         if (Freelist->next == NULL)         {             break;         }         */     }     if (strcmp(Freelist->x, a) == 0)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");         return;     }     //else         printf("没有相关数据！无法删除！！\n"); }  供参考～  楼主的程序只能创建一个节点和删除一个节点。这个比较好就是这个改成这样好像会跳过

user: user-1
content: //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     //if (Freelist->next == NULL)     if (Freelist == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     //while (Freelist->x != a)     while (Freelist && strcmp(Freelist->x, a) != 0)     {         SFreelist = Freelist;         Freelist = Freelist->next;         /*         if (Freelist->next == NULL)         {             break;         }         */     }     if (strcmp(Freelist->x, a) == 0)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");         return;     }     //else         printf("没有相关数据！无法删除！！\n"); }  供参考～  楼主的程序只能创建一个节点和删除一个节点。

user: user-0
content: 引用 4 楼 自信男孩的回复:你是说测试的结果一样吗？ 结果是的，不知道为什么

user: user-0
content: 早上刚起来还以为我自己写的注释打开电脑发现是改了

user: user-1
content: 你是说测试的结果一样吗？

user: user-1
content: 引用 2 楼 int i； 的回复:[quote=引用 1 楼 自信男孩的回复:]#define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <malloc.h> # include <string.h>  struct studant {     char x[50];     struct studant* next; };  //创建链表 <2>   靠插入来完成链表 struct studant* Apex() {     struct studant* AApex = (struct studant*)malloc(sizeof(struct studant));     AApex->next = NULL;      return AApex; }  //创建链表 <1>    先制作链表再来操作链表 struct studant* apex(void) {     int i, g;     struct studant* papex;     papex = (struct studant*)malloc(sizeof(struct studant));     papex->next = NULL;      struct studant* aapex = papex;     aapex->next = NULL;      printf("需要多少个链表：");     scanf("%d", &g);     while (getchar() != '\n');     for (i = 0; i < g; i++)     {         struct studant* paapex = (struct studant*)malloc(sizeof(struct studant));         paapex->next = NULL;         printf("输入第%d个链表内的数据：", i + 1);         scanf("%s", paapex->x);         while (getchar() != '\n');         paapex->next = aapex->next;         aapex->next = paapex;     }     return papex; }  //插入节点 struct studant* zjlist(struct studant* ppapex, char a[50]) {     struct studant* qqapex;     qqapex = (struct studant*)malloc(sizeof(struct studant));     strcpy(qqapex->x, a);     qqapex->next = ppapex->next;     ppapex->next = qqapex;     return qqapex; }  //函数 头插法插入链表（增） void sclist(struct studant* llist) {     char z;     char zz[20];     int i = 1;     printf("是否开始(y/n):");     scanf("%c", &z);     while (getchar() != '\n');     if (z == 'y' || z == 'Y')         while (z == 'y' || z == 'Y')         {             printf("\n链表%d\n", i++);             printf("输入数据：");             //scanf("%s", &zz);             scanf("%s", zz);             while (getchar() != '\n');             zjlist(llist, zz);             printf("\n是否继续(y/n)：");             scanf("%c", &z);             while (getchar() != '\n')                 ;             //if (z == 'y' || z == 'Y');   //判断是否继续创建插入链表             if (z != 'y' || z != 'Y')    //判断是否继续创建插入链表                 break;         } }  //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 {     struct studant* Freelist = freelist->next;     struct studant* SFreelist = freelist;     if (Freelist->next == NULL)     {         printf("链表为空！无法删除！！\n");         return;     }     while (Freelist->x != a)     {         SFreelist = Freelist;         Freelist = Freelist->next;         if (Freelist->next == NULL)         {             break;         }     }     if (Freelist->x == a)     {         SFreelist->next = Freelist->next;         free(Freelist);         printf("\n成功删除！！\n");     }     else         printf("没有相关数据！无法删除！！\n"); }  //打印遍历链表 void bllist(struct studant* bllist) {     struct studant* bblist = bllist->next;//让指针指向头结点的地址然后从下一个节点开始打印（因为头结点不存放数据  会乱码  ）     if (bblist != NULL)         while (bblist != NULL)         {             printf("%s\n", bblist->x);             bblist = bblist->next;         }     else         printf("链表为空！\n"); }  int main(void) {     char a;     char v[50];     struct studant* list = Apex(); //apex();     bllist(list);//遍历链表     //     printf("要增加链表吗(y/n)：");     scanf("%c", &a);     while (getchar() != '\n')         ;     if (a == 'y' || a == 'Y')     {         sclist(list);         bllist(list);     }     //     printf("\n要删除链表吗(y/n)：");     scanf("%c", &a);     while (getchar() != '\n');     if (a == 'y' || a == 'Y')     {         printf("输入要删除的数据：");         //scanf("%s", &v);         scanf("%s", v);         shanlist(list, v);     }     //     printf("是否要再次遍历(y/n)：");     scanf("%c", &a);     printf("\n");     while (getchar() != '\n');     if (a == 'y' || a == 'Y')     {         bllist(list);     }      return 0; } 供参考～ 好像一样啊怎么参考[/quote]  代码一样吗？ 我看着是修改了呢？  //scanf("%s", &v);         scanf("%s", v);