鱼C论坛

 找回密码
 立即注册
查看: 5264|回复: 8

[已解决]c代码输出是乱码,请各位大神帮忙看看是哪有问题

[复制链接]
发表于 2021-7-11 12:10:39 | 显示全部楼层 |阅读模式
2鱼币
题目是
有两个连边a和b,设结点中包含学号、姓名。从a链表中删去与b链表中相同学号的那些结点
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100

  4. typedef struct Student
  5. {
  6.         int num;
  7.         char name[MAXSIZE];
  8.         struct Student *next;
  9. }Student;

  10. int n;

  11. Student* CreateLink(Student *P)
  12. {
  13.         Student *P1,*head;
  14.         n=0;
  15.        
  16.         P=P1=(Student *)malloc(sizeof(Student));
  17.         head=NULL;
  18.         printf("input Number:");
  19.         scanf("%d",&P->num);
  20.         getchar();
  21.         if(P->num != 0)
  22.         {
  23.                 printf("input Name:");
  24.                 scanf("%s",P->name);
  25.                 getchar();
  26.         }
  27.         while(P->num != 0)
  28.         {
  29.                 n++;
  30.                 if(n==1)
  31.                 {
  32.                         head=P;
  33.                 }
  34.                 else
  35.                 {
  36.                         P1->next=P;
  37.                 }
  38.                 P1=P;
  39.                 P=(Student *)malloc(sizeof(Student));
  40.                 printf("input Number:");
  41.                 scanf("%d",&P->num);
  42.                 getchar();
  43.                 if(P->num != 0)
  44.                 {
  45.                         printf("input Name:");
  46.                         scanf("%s",P->name);
  47.                         getchar();
  48.                 }
  49.         }
  50.         P1->next=NULL;
  51.         return (head);
  52. }
  53. Student* Del(Student *A,Student *B)
  54. {
  55.         Student *head,*P;
  56.         head=A;
  57.         for(;B != NULL;B->next);
  58.         {
  59.                 while(A)
  60.                 {
  61.                         if(A->num == B->num)
  62.                         {
  63.                                 if(A==head)
  64.                                         head=A->next;
  65.                                 else
  66.                                 {
  67.                                 P->next=A->next;
  68.                                 A=A->next;
  69.                                 }
  70.                         }
  71.                         else
  72.                         {
  73.                                 P=A;
  74.                                 A=A->next;
  75.                         }
  76.                 }
  77.         }
  78.         return (head);
  79. }

  80. void print(Student *P)
  81. {
  82.         Student *head;
  83.         head=P;
  84.         while(head!= NULL)
  85.         {
  86.                 printf("No. %d   Name: %s\n",head->num,head->name);
  87.                 head=head->next;
  88.         }
  89. }

  90. int main()
  91. {
  92.         Student A,B;
  93.         CreateLink(&A);
  94.         printf("The Graph A is:\n");
  95.         print(&A);
  96.         CreateLink(&B);
  97.         printf("The Graph B is:\n");
  98.         print(&B);
  99.         Del(&A,&B);
  100.         printf("The New Graph A is:\n");
  101.         print(&A);
  102.         return 0;
  103. }
复制代码



最佳答案
2021-7-11 12:10:40
我测试了下,创建链表那没问题,
有问题应该是以下两个地方:
①return (head);
②CreateLink(&A);
即,参数返回或者传参的问题。
fffsfs.jpg

最佳答案

查看完整内容

我测试了下,创建链表那没问题, 有问题应该是以下两个地方: ①return (head); ②CreateLink(&A); 即,参数返回或者传参的问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 12:10:40 | 显示全部楼层    本楼为最佳答案   
我测试了下,创建链表那没问题,
有问题应该是以下两个地方:
①return (head);
②CreateLink(&A);
即,参数返回或者传参的问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 12:27:05 | 显示全部楼层
你的编译器什么提示也没有?那一定要换一个好一点的编译器
  1. $ gcc -g -Wall -o main main.c
  2. main.c: In function ‘Del’:
  3. main.c:60:25: warning: statement with no effect [-Wunused-value]
  4.    60 |         for(;B != NULL;B->next);
  5.       |                        ~^~~~~~
  6. main.c:60:9: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
  7.    60 |         for(;B != NULL;B->next);
  8.       |         ^~~
  9. main.c:61:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
  10.    61 |         {
  11.       |         ^
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 12:27:46 From FishC Mobile | 显示全部楼层
人造人 发表于 2021-7-11 12:27
你的编译器什么提示也没有?那一定要换一个好一点的编译器

一看就是古董vc60
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 12:28:05 | 显示全部楼层
wp231957 发表于 2021-7-11 12:27
一看就是古董vc60

^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 12:46:40 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100

  4. typedef struct Student
  5. {
  6.         int num;
  7.         char name[MAXSIZE];
  8.         struct Student *next;
  9. }Student;

  10. int n;

  11. Student* CreateLink(Student *P)
  12. {
  13.         Student *P1,*head;
  14.         n=0;

  15.         P=P1=(Student *)malloc(sizeof(Student));
  16.         head=NULL;
  17.         printf("input Number:");
  18.         scanf("%d",&P->num);
  19.         getchar();
  20.         if(P->num != 0)
  21.         {
  22.                 printf("input Name:");
  23.                 scanf("%s",P->name);
  24.                 getchar();
  25.         }
  26.         while(P->num != 0)
  27.         {
  28.                 n++;
  29.                 if(n==1)
  30.                 {
  31.                         head=P;
  32.                 }
  33.                 else
  34.                 {
  35.                         P1->next=P;
  36.                 }
  37.                 P1=P;
  38.                 P=(Student *)malloc(sizeof(Student));
  39.                 printf("input Number:");
  40.                 scanf("%d",&P->num);
  41.                 getchar();
  42.                 if(P->num != 0)
  43.                 {
  44.                         printf("input Name:");
  45.                         scanf("%s",P->name);
  46.                         getchar();
  47.                 }
  48.         }
  49.         P1->next=NULL;
  50.         return (head);
  51. }
  52. Student* Del(Student *A,Student *B)
  53. {
  54.         Student *head,*P;
  55.         head=A;
  56.         //for(;B != NULL;B->next);
  57.         for(;B != NULL; B = B->next)
  58.         {
  59.                 while(A)
  60.                 {
  61.                         if(A->num == B->num)
  62.                         {
  63.                                 if(A==head)
  64.                                         head=A->next;
  65.                                 else
  66.                                 {
  67.                                         P->next=A->next;
  68.                                         A=A->next;
  69.                                 }
  70.                         }
  71.                         else
  72.                         {
  73.                                 P=A;
  74.                                 A=A->next;
  75.                         }
  76.                 }
  77.         }
  78.         return (head);
  79. }

  80. void print(Student *P)
  81. {
  82.         Student *head;
  83.         head=P;
  84.         while(head!= NULL)
  85.         {
  86.                 printf("No. %d   Name: %s\n",head->num,head->name);
  87.                 head=head->next;
  88.         }
  89. }

  90. int main()
  91. {
  92.         Student A,B;
  93.         CreateLink(&A);
  94.         printf("The Graph A is:\n");
  95.         print(&A);
  96.         CreateLink(&B);
  97.         printf("The Graph B is:\n");
  98.         print(&B);
  99.         Del(&A,&B);
  100.         printf("The New Graph A is:\n");
  101.         print(&A);
  102.         return 0;
  103. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 13:43:09 | 显示全部楼层
wp231957 发表于 2021-7-11 12:27
一看就是古董vc60

这不是我之前那个出问题的老古董吗
至今没搞明白啊哈哈  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-11 13:46:18 From FishC Mobile | 显示全部楼层
Gacy 发表于 2021-7-11 13:43
这不是我之前那个出问题的老古董吗
至今没搞明白啊哈哈

vc60不应该被使用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-12 21:45:36 | 显示全部楼层
wp231957 发表于 2021-7-11 13:46
vc60不应该被使用

那为什么它还存在呢 猫猫头
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-30 18:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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