c语言链表输出链表出现-842150451
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#defineNULL0#defineLENsizeof(str...
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int number;
char name[20];
struct student * next;
};
void main()
{ void print(struct student * head);
struct student * creat();
struct student * p3;
p3=creat();
print(p3);
}
struct student * creat(void)
{
struct student * p1,*p2;
struct student * head;
int i,n=0;
FILE * fp;
if((fp=fopen("name2.txt","r"))==NULL)
{
printf("cannot open");
exit(0);
}
p1=p2=(struct student *)malloc(LEN);
i=0;
while(n<30)
{
i=i+1;
if(i>1)
{
p2->next=p1;
p2=p1;
}
else head=p1;
p1=(struct student * )malloc(LEN);
fscanf(fp,"%d%c\n",&p1->number,&p1->name);//fatal problem
n++;
}
p2->next=NULL;
fclose(fp);
return(head);
}
void print(struct student * head)
{
struct student * p;
p=head;
if(head!=NULL)
{
do
{printf("%d %c\n",p->number,p->name);
p=p->next;}
while(p!=NULL);
}
} 展开
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int number;
char name[20];
struct student * next;
};
void main()
{ void print(struct student * head);
struct student * creat();
struct student * p3;
p3=creat();
print(p3);
}
struct student * creat(void)
{
struct student * p1,*p2;
struct student * head;
int i,n=0;
FILE * fp;
if((fp=fopen("name2.txt","r"))==NULL)
{
printf("cannot open");
exit(0);
}
p1=p2=(struct student *)malloc(LEN);
i=0;
while(n<30)
{
i=i+1;
if(i>1)
{
p2->next=p1;
p2=p1;
}
else head=p1;
p1=(struct student * )malloc(LEN);
fscanf(fp,"%d%c\n",&p1->number,&p1->name);//fatal problem
n++;
}
p2->next=NULL;
fclose(fp);
return(head);
}
void print(struct student * head)
{
struct student * p;
p=head;
if(head!=NULL)
{
do
{printf("%d %c\n",p->number,p->name);
p=p->next;}
while(p!=NULL);
}
} 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询