鱼C论坛

 找回密码
 立即注册
查看: 2065|回复: 6

[已解决]这个为什么、 一输入0就错误?scanf不是可以接受0,吗?

[复制链接]
发表于 2022-12-15 19:16:52 | 显示全部楼层 |阅读模式
20鱼币
203304wopiawi11hxa88wi.png.thumb.png
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.         int num;
  5.         struct Node *next;
  6. };

  7. void function(void);
  8. void ds_init(struct Node **head);
  9. int ds_insert(struct Node **head, int i);
  10. void ds_delete(struct Node **head, int i);
  11. int ds_search(struct Node *head, int i);
  12. void print(struct Node *head);
  13. int ClerrList(struct Node **head);

  14. void function(void){
  15.         printf("1.初始化链表\n");
  16.         printf("2.插入结点\n");
  17.         printf("3.删除结点\n");
  18.         printf("4.查找结点\n");
  19.         printf("5.遍历链表\n");
  20.         printf("0.退出\n");
  21. }

  22. void ds_init(struct Node **head){
  23.         int i;
  24.         struct Node *node;
  25.         struct Node *temp;

  26.         printf("输入结点的值,输入-1完成初始化\n");

  27.         while(1){
  28.                 scanf("%d", &i);
  29.                 if (i == -1){
  30.                         return ;
  31.                 }

  32.                 if (!(*head)){//        !(*head) --> *head == NULL
  33.                         *head = (struct Node *)malloc(sizeof(struct Node));
  34.                         if(!(*head)){
  35.                                 exit(1);
  36.                         }
  37.                         (*head)->num = i;
  38.                         (*head)->next = *head;
  39.                 }
  40.                 else{
  41.                         for (node = (*head); node->next != (*head); node = node->next)
  42.                                 ;
  43.                         temp = (struct Node *)malloc(sizeof(struct Node));
  44.                         if(!(*head)){;
  45.                                 exit(1);
  46.                         }

  47.                         temp->num = i;
  48.                         temp->next = *head;
  49.                         node->next = temp;
  50.                 }
  51.         }
  52. }

  53. int ds_insert(struct Node **head, int i){
  54.         int j, k = 1;
  55.         struct Node *node;
  56.         struct Node *now;
  57.         struct Node *temp;

  58.         printf("请输入要插入的结点的值");
  59.         scanf("%d", &j);

  60.         if (i == 1){//新插入的结点作为第一个结点
  61.                 now = (struct Node *)malloc(sizeof(struct Node));
  62.                 if(!now){
  63.                         exit(1);
  64.                 }
  65.                 now->num = j;
  66.                 for (node = *head; node->next != *head; node = node->next)//找到最后一个结点
  67.                         ;

  68.                 now->next = *head;
  69.                 node->next = now;//temp放到head前面
  70.                 *head = now; //head要指向第一个结点
  71.         }
  72.         else{
  73.                 node = *head;

  74.                 while ( ++k < i){
  75.                         node = node->next;
  76.                 }

  77.                 now = (struct Node *)malloc(sizeof(struct Node));
  78.                 if(!now){
  79.                         exit(1);
  80.                 }
  81.                 now->num = j;

  82.                 temp = node->next;
  83.                 node->next = now;
  84.                 now->next = temp;
  85.         }
  86.         return j;
  87. }

  88. void ds_delete(struct Node **head, int i){
  89.         int j = 1;
  90.         struct Node *node;
  91.         struct Node *temp;

  92.         if (i == 1){//删除的第一个结点
  93.                 for (node = *head; node->next != (*head); node = node->next)//找到最后一个结点
  94.                         ;

  95.                 temp = *head;
  96.                 *head = (*head)->next;
  97.                 node->next = *head;
  98.                 free(temp);
  99.         }
  100.         else{
  101.                 node = *head;

  102.                 while ( ++j < i){
  103.                         node = node->next;
  104.                 }

  105.                 temp = node->next;
  106.                 node->next = temp->next;
  107.                 free(temp);
  108.         }
  109. }

  110. int ds_search(struct Node *head, int i){
  111.         int j = 1;
  112.         struct Node *node;

  113.         for (node = head; node->next != head && node->num != i; j++){//找到最后一个结点
  114.                 node = node->next;
  115.         }

  116.         if (node->next == head)
  117.                 return 0;
  118.         else
  119.                 return j;
  120. }

  121. void print(struct Node *head){//遍历
  122.         struct Node *node;
  123.         int i = 0;

  124.         node = head;

  125.         do{
  126.                 printf("%d ", node->num);
  127.                 node = node->next;
  128.         }while (node != head);
  129.         putchar('\n');
  130. }

  131. int ClerrList(struct Node **head){//将单链表清空
  132.         struct Node *node, *temp;

  133.         node = *head;
  134.         while (node){
  135.                 temp = node;
  136.                 node = node->next;
  137.                 free(temp);
  138.                 /*temp = node->next;
  139.                 free(node);
  140.                 node = temp;*/
  141.         }
  142.         if (node)
  143.                 return 0;
  144.         else
  145.                 (*head)->next = NULL;

  146.         return 1;
  147. }

  148. int main(){
  149.         char i;
  150.         int j;
  151.         struct Node *head = NULL;

  152.         function();
  153.         while (1){
  154.                 printf("请输入指令:");
  155.                 scanf("%c", &i);
  156.                 printf("__________");
  157.                 switch (i){
  158.                         case '1': ds_init(&head);
  159.                                 print(head);break;
  160.                         case '2': printf("请输入要插入的位置:");
  161.                                 scanf("%d", &j);
  162.                                 printf("再位置%d插入%d后:", j, ds_insert(&head, j));
  163.                                 print(head);break;
  164.                         case '3': printf("请输入要删除的位置:");
  165.                                 scanf("%d", &j);
  166.                                 ds_delete(&head, j);
  167.                                 printf("删除第%d个结点后:", j);
  168.                                 print(head);break;
  169.                         case '4': printf("请输入查找的值:");
  170.                                 scanf("%d", &j);
  171.                                 printf("元素%d所在的位置:%d\n", j, ds_search(head, j));break;
  172.                         case '5': print(head);break;
  173.                         case '0': ClerrList(&head);exit(1);
  174.                 }
  175.         }

  176.         return 0;
  177. }
复制代码
最佳答案
2022-12-15 19:16:53
  1. int ClerrList(struct Node **head){//将单链表清空
  2.         struct Node *node, *temp;

  3.         node = *head;
  4.         while (node){//你怎么改成node->next
  5.                 temp = node;
  6.                 node = node->next;
  7.                 free(temp);
  8.                 /*temp = node->next;
  9.                 free(node);
  10.                 node = temp;*/
  11.         }
  12.         if (node)
  13.                 (*head)->next = NULL;
  14.         else
  15.                 return 0;
  16.         return 1;
  17. }
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 19:16:53 | 显示全部楼层    本楼为最佳答案   
  1. int ClerrList(struct Node **head){//将单链表清空
  2.         struct Node *node, *temp;

  3.         node = *head;
  4.         while (node){//你怎么改成node->next
  5.                 temp = node;
  6.                 node = node->next;
  7.                 free(temp);
  8.                 /*temp = node->next;
  9.                 free(node);
  10.                 node = temp;*/
  11.         }
  12.         if (node)
  13.                 (*head)->next = NULL;
  14.         else
  15.                 return 0;
  16.         return 1;
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 19:40:29 | 显示全部楼层
问题不在scanf那里,在ClerrList这个函数里,那个函数写的有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-15 20:14:57 | 显示全部楼层
lvk 发表于 2022-12-15 19:40
问题不在scanf那里,在ClerrList这个函数里,那个函数写的有问题

可是那个printf都没有执行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 20:15:27 | 显示全部楼层
  1. if (node)
  2.                (*head)->next = NULL;
  3.         else
  4.                return 0;
  5.                
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-15 20:21:09 | 显示全部楼层

KO`LAB6}O]MVC11~UZP~KL7.png 还是不行
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.         int num;
  5.         struct Node *next;
  6. };

  7. void function(void);
  8. void ds_init(struct Node **head);
  9. int ds_insert(struct Node **head, int i);
  10. void ds_delete(struct Node **head, int i);
  11. int ds_search(struct Node *head, int i);
  12. void print(struct Node *head);
  13. int ClerrList(struct Node **head);

  14. void function(void){
  15.         printf("1.初始化链表\n");
  16.         printf("2.插入结点\n");
  17.         printf("3.删除结点\n");
  18.         printf("4.查找结点\n");
  19.         printf("5.遍历链表\n");
  20.         printf("0.退出\n");
  21. }

  22. void ds_init(struct Node **head){
  23.         int i;
  24.         struct Node *node;
  25.         struct Node *temp;

  26.         printf("输入结点的值,输入-1完成初始化\n");

  27.         while(1){
  28.                 scanf("%d", &i);
  29.                 if (i == -1){
  30.                         return ;
  31.                 }

  32.                 if (!(*head)){//        !(*head) --> *head == NULL
  33.                         *head = (struct Node *)malloc(sizeof(struct Node));
  34.                         if(!(*head)){
  35.                                 exit(1);
  36.                         }
  37.                         (*head)->num = i;
  38.                         (*head)->next = *head;
  39.                 }
  40.                 else{
  41.                         for (node = (*head); node->next != (*head); node = node->next)
  42.                                 ;
  43.                         temp = (struct Node *)malloc(sizeof(struct Node));
  44.                         if(!(*head)){;
  45.                                 exit(1);
  46.                         }

  47.                         temp->num = i;
  48.                         temp->next = *head;
  49.                         node->next = temp;
  50.                 }
  51.         }
  52. }

  53. int ds_insert(struct Node **head, int i){
  54.         int j, k = 1;
  55.         struct Node *node;
  56.         struct Node *now;
  57.         struct Node *temp;

  58.         printf("请输入要插入的结点的值");
  59.         scanf("%d", &j);

  60.         if (i == 1){//新插入的结点作为第一个结点
  61.                 now = (struct Node *)malloc(sizeof(struct Node));
  62.                 if(!now){
  63.                         exit(1);
  64.                 }
  65.                 now->num = j;
  66.                 for (node = *head; node->next != *head; node = node->next)//找到最后一个结点
  67.                         ;

  68.                 now->next = *head;
  69.                 node->next = now;//temp放到head前面
  70.                 *head = now; //head要指向第一个结点
  71.         }
  72.         else{
  73.                 node = *head;

  74.                 while ( ++k < i){
  75.                         node = node->next;
  76.                 }

  77.                 now = (struct Node *)malloc(sizeof(struct Node));
  78.                 if(!now){
  79.                         exit(1);
  80.                 }
  81.                 now->num = j;

  82.                 temp = node->next;
  83.                 node->next = now;
  84.                 now->next = temp;
  85.         }
  86.         return j;
  87. }

  88. void ds_delete(struct Node **head, int i){
  89.         int j = 1;
  90.         struct Node *node;
  91.         struct Node *temp;

  92.         if (i == 1){//删除的第一个结点
  93.                 for (node = *head; node->next != (*head); node = node->next)//找到最后一个结点
  94.                         ;

  95.                 temp = *head;
  96.                 *head = (*head)->next;
  97.                 node->next = *head;
  98.                 free(temp);
  99.         }
  100.         else{
  101.                 node = *head;

  102.                 while ( ++j < i){
  103.                         node = node->next;
  104.                 }

  105.                 temp = node->next;
  106.                 node->next = temp->next;
  107.                 free(temp);
  108.         }
  109. }

  110. int ds_search(struct Node *head, int i){
  111.         int j = 1;
  112.         struct Node *node;

  113.         for (node = head; node->next != head && node->num != i; j++){//找到最后一个结点
  114.                 node = node->next;
  115.         }

  116.         if (node->next == head)
  117.                 return 0;
  118.         else
  119.                 return j;
  120. }

  121. void print(struct Node *head){//遍历
  122.         struct Node *node;
  123.         int i = 0;

  124.         node = head;

  125.         do{
  126.                 printf("%d ", node->num);
  127.                 node = node->next;
  128.         }while (node != head);
  129.         putchar('\n');
  130. }

  131. int ClerrList(struct Node **head){//将单链表清空
  132.         struct Node *node, *temp;

  133.         node = *head;
  134.         while (node->next){
  135.                 temp = node;
  136.                 node = node->next;
  137.                 free(temp);
  138.                 /*temp = node->next;
  139.                 free(node);
  140.                 node = temp;*/
  141.         }
  142.         if (node)
  143.                 (*head)->next = NULL;
  144.         else
  145.                 return 0;
  146.         return 1;
  147. }

  148. int main(){
  149.         char i;
  150.         int j;
  151.         struct Node *head = NULL;

  152.         function();
  153.         while (1){
  154.                 printf("请输入指令:");
  155.                 scanf("%c", &i);
  156.                 switch (i){
  157.                         case '1': ds_init(&head);
  158.                                 print(head);break;
  159.                         case '2': printf("请输入要插入的位置:");
  160.                                 scanf("%d", &j);
  161.                                 printf("再位置%d插入%d后:", j, ds_insert(&head, j));
  162.                                 print(head);break;
  163.                         case '3': printf("请输入要删除的位置:");
  164.                                 scanf("%d", &j);
  165.                                 ds_delete(&head, j);
  166.                                 printf("删除第%d个结点后:", j);
  167.                                 print(head);break;
  168.                         case '4': printf("请输入查找的值:");
  169.                                 scanf("%d", &j);
  170.                                 printf("元素%d所在的位置:%d\n", j, ds_search(head, j));break;
  171.                         case '5': print(head);break;
  172.                         case '0': ClerrList(&head);exit(1);
  173.                 }
  174.         }

  175.         return 0;
  176. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 20:22:51 | 显示全部楼层
御坂19090 发表于 2022-12-15 20:14
可是那个printf都没有执行

错误到底是不是在ClerrList这个函数里,你调试一下,一步步执行不就知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 22:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表