c代码输出是乱码,请各位大神帮忙看看是哪有问题
题目是有两个连边a和b,设结点中包含学号、姓名。从a链表中删去与b链表中相同学号的那些结点
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct Student
{
int num;
char name;
struct Student *next;
}Student;
int n;
Student* CreateLink(Student *P)
{
Student *P1,*head;
n=0;
P=P1=(Student *)malloc(sizeof(Student));
head=NULL;
printf("input Number:");
scanf("%d",&P->num);
getchar();
if(P->num != 0)
{
printf("input Name:");
scanf("%s",P->name);
getchar();
}
while(P->num != 0)
{
n++;
if(n==1)
{
head=P;
}
else
{
P1->next=P;
}
P1=P;
P=(Student *)malloc(sizeof(Student));
printf("input Number:");
scanf("%d",&P->num);
getchar();
if(P->num != 0)
{
printf("input Name:");
scanf("%s",P->name);
getchar();
}
}
P1->next=NULL;
return (head);
}
Student* Del(Student *A,Student *B)
{
Student *head,*P;
head=A;
for(;B != NULL;B->next);
{
while(A)
{
if(A->num == B->num)
{
if(A==head)
head=A->next;
else
{
P->next=A->next;
A=A->next;
}
}
else
{
P=A;
A=A->next;
}
}
}
return (head);
}
void print(Student *P)
{
Student *head;
head=P;
while(head!= NULL)
{
printf("No. %d Name: %s\n",head->num,head->name);
head=head->next;
}
}
int main()
{
Student A,B;
CreateLink(&A);
printf("The Graph A is:\n");
print(&A);
CreateLink(&B);
printf("The Graph B is:\n");
print(&B);
Del(&A,&B);
printf("The New Graph A is:\n");
print(&A);
return 0;
}
我测试了下,创建链表那没问题,
有问题应该是以下两个地方:
①return (head);
②CreateLink(&A);
即,参数返回或者传参的问题。 你的编译器什么提示也没有?那一定要换一个好一点的编译器
$ gcc -g -Wall -o main main.c
main.c: In function ‘Del’:
main.c:60:25: warning: statement with no effect [-Wunused-value]
60 | for(;B != NULL;B->next);
| ~^~~~~~
main.c:60:9: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
60 | for(;B != NULL;B->next);
| ^~~
main.c:61:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
61 | {
| ^
人造人 发表于 2021-7-11 12:27
你的编译器什么提示也没有?那一定要换一个好一点的编译器
一看就是古董vc60 wp231957 发表于 2021-7-11 12:27
一看就是古董vc60
^_^ #include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct Student
{
int num;
char name;
struct Student *next;
}Student;
int n;
Student* CreateLink(Student *P)
{
Student *P1,*head;
n=0;
P=P1=(Student *)malloc(sizeof(Student));
head=NULL;
printf("input Number:");
scanf("%d",&P->num);
getchar();
if(P->num != 0)
{
printf("input Name:");
scanf("%s",P->name);
getchar();
}
while(P->num != 0)
{
n++;
if(n==1)
{
head=P;
}
else
{
P1->next=P;
}
P1=P;
P=(Student *)malloc(sizeof(Student));
printf("input Number:");
scanf("%d",&P->num);
getchar();
if(P->num != 0)
{
printf("input Name:");
scanf("%s",P->name);
getchar();
}
}
P1->next=NULL;
return (head);
}
Student* Del(Student *A,Student *B)
{
Student *head,*P;
head=A;
//for(;B != NULL;B->next);
for(;B != NULL; B = B->next)
{
while(A)
{
if(A->num == B->num)
{
if(A==head)
head=A->next;
else
{
P->next=A->next;
A=A->next;
}
}
else
{
P=A;
A=A->next;
}
}
}
return (head);
}
void print(Student *P)
{
Student *head;
head=P;
while(head!= NULL)
{
printf("No. %d Name: %s\n",head->num,head->name);
head=head->next;
}
}
int main()
{
Student A,B;
CreateLink(&A);
printf("The Graph A is:\n");
print(&A);
CreateLink(&B);
printf("The Graph B is:\n");
print(&B);
Del(&A,&B);
printf("The New Graph A is:\n");
print(&A);
return 0;
}
wp231957 发表于 2021-7-11 12:27
一看就是古董vc60
这不是我之前那个出问题的老古董吗{:10_266:}{:10_254:}
至今没搞明白啊哈哈 Gacy 发表于 2021-7-11 13:43
这不是我之前那个出问题的老古董吗
至今没搞明白啊哈哈
vc60不应该被使用 wp231957 发表于 2021-7-11 13:46
vc60不应该被使用
那为什么它还存在呢 猫猫头{:10_254:}
页:
[1]