一个链表小程序.帮看一下.

来源:百度知道 编辑:UC知道 时间:2024/05/14 19:39:54
#include <stdio.h>
#include<conio.h>
#include <malloc.h>

void main()
{
typedef struct student
{int num;
char name[10];
char sex;
struct student *next;
} stu;
stu *head,*p1,*p2;
p1=(stu *)malloc(sizeof(stu));
p2=p1;
printf("请输入信息:");
scanf("%d %s %c",&p1->num,p1->name,&p1->sex);
head=NULL;
int n=0;
//输入数据,以0结束!
while(p1->num!=0)
{n+=1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(stu *)malloc(sizeof(stu));
scanf("%d %s %c",&p1->num,p1->name,&p1->sex);

}
p1->next=NULL;
//输出,
p1=head;

do {
printf("%d %s %c\n",p1->num,p1->name,p1->sex);
p1=p1->next;
} while(p1!=NULL);
getch();

}
输入正常.但是内存不有READ.谢谢大家帮我看一下.
输出正常

#include <stdio.h>
#include<conio.h>
#include <malloc.h>

void main()
{
typedef struct student
{
int num;
char name[10];
char sex;
struct student *next;
} stu;
stu *head,*p1,*p2;
//p1=(stu *)malloc(sizeof(stu));
//p2=p1;
printf("请输入信息:");
//scanf("%d %s %c",&p1->num,p1->name,&p1->sex);
head=NULL;
int n=0;
//输入数据,以0结束!
do
{
p1=(stu *)malloc(sizeof(stu));
n+=1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
scanf("%d %s %c",&p1->num,p1->name,&p1->sex);
} while(p1->num!=0) ;
p1->next=NULL;
//输出,
p1=head;
do {
printf("%d %s %c\n",p1->num,p1->name,p1->sex);
p1=p1->next;
} while(p1!=NULL);
getch();
}

使用了没有初始化的指