c语言的链表问题。。请帮忙看下出什么问题了

来源:百度知道 编辑:UC知道 时间:2024/09/23 06:28:06
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
char num[10];
char name[20];
char sex[2];
int age;
struct student *next;
};
struct student *creat()
{
struct student *head,*p1,*p2;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
printf("请输入学生的信息:\n");
printf("num:");
scanf("%s",&p1->num);
printf("name:");
scanf("%s",&p1->name);
printf("sex:");
scanf("%s",&p1->sex);
printf("age:");
scanf("%d",&p1->age);
while(p2->num!=0)
{
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("num:");
scanf("%s",&p1->num);
printf("name:");

问题的原因在于“num"是作为字符串输入的,故不可以这样写:while(p2->num!=0)
应该改成这样:while(strcmp(p2->num,"0")) 再在加上头文件#include<string.h>就可以了

以后若有什么问题直接问我啊,我也是初学的,想同大家交流一下,呵呵

#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
char num[10];
char name[20];
char sex[2];
int age;
struct student *next;
};
struct student *creat()
{
struct student *head,*p1,*p2;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
printf("请输入学生的信息:\n");
printf("num:");
scanf("%s",&p1->num);
printf("name:");
scanf("%s",&p1->name);
printf("sex:");
scanf("%s",&p1->sex);
printf("age:");
scanf("%d",&p1->age);
while(p2->num!=0)//因此处不会为空,所以循环不会结束。
{ /