C语言 如何解决文件的问题.exe在链表中插入一次后停止运行


#include<stdio.h>
#include<stdlib.h>
struct node{
int id;
char name[50];
float point;
char grade;
struct node *next;
};
struct node *head;
void DataEntry(struct node *);
void GetData(struct node *);
void DataDisp();
void Insert()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
DataEntry(p);
if(head==NULL)
{
head=p;
}
else
{
struct node *q;
q=head;
while(q!=NULL)
{
q=q->next;
}
q->next=p;
p->next=NULL;
}

}
void DataEntry(struct node *p)
{
printf("Enter value of Id employeen");
scanf("%d",&p->id);
printf("Enter name of the personn");
scanf("%s",p->name);
printf("Enter points got by Employeen");
scanf("%f",&p->point);
printf("Enter grade of the personn");
fflush(stdin);
scanf("%c",&p->grade);
}
void GetData(struct node *q)
{
printf("The data entered by you is as followsn");
printf("Id is %dn",q->id);
printf("Name is %sn",q->name);
printf("Point is %fn",q->point);
printf("Grade is %cn",q->grade);
}
void DataDisp()
{
struct node *z;
z=head;
if(z==NULL)
{
printf("List is Emptyn");
return;
}
while(z!=NULL)
{
GetData(z);
z=z->next;
}
}
int main()
{
Insert();
DataDisp();
}

我已经实现了一个单向链表,我使用 Insert(( 函数将数据插入列表,并在 DataDisp(( 函数中使用 DataDisp(( 函数显示数据,我使用了 GetData(( 函数,该函数正在访问来自链表节点的数据。这里的问题是我可以进行第一次插入,它也使用 DataDisp(( 显示数据,但之后,出现错误文件.exe停止运行。我认为存在分割错误;我尽力解决问题,但无法成功运行。请帮忙。

还有一件事是尽量防止使用"FFLUSH"关键字。这不是清理输入缓冲区的可移植方法,并且可能不适用于所有编译器。

最新更新