链表出现 Null pointer assignment 希望各位高手帮忙解决,麻烦大家了
程序很长,麻烦大家了#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 40
/* here is the declaration of a linked list structure */
struct Stdinfo
{
char num[MAX]; /* for students number */
char names[MAX]; /* for students name */
float score;
struct Stdinfo *nextAddr;
};
/* here is the definition of the first structure pointer */
struct Stdinfo *tosp;
void main()
{
FILE *outfile;
int select;
struct Stdinfo *newaddr;
void add_record(struct Stdinfo *),delet_record(struct Stdinfo *),check_scores(struct Stdinfo *),recompose_scores(struct Stdinfo *),save_record(struct Stdinfo *),check(struct Stdinfo *);
tosp=NULL;
outfile=fopen("c:\\record.txt","r"); /* read the information from the file */
fseek(outfile,0L,SEEK_SET);
if(outfile==NULL||filelength(outfile)==0)
{
printf("\nThere is not any record.");
printf("\nPlease enter the students' information first.");
}
while(!feof(outfile))
{
newaddr=(struct Stdinfo *) malloc(sizeof(struct Stdinfo));
fread(newaddr,sizeof(struct Stdinfo),1,outfile);
newaddr->nextAddr=tosp;
tosp=newaddr;
}
tosp=tosp->nextAddr;
fclose(outfile);
printf("\n********************************************************************************");
printf("\nEnter a select code:"); /* let user make a choice */
printf("\n1 for adding new records.");
printf("\n2 for deleting new records.");
printf("\n3 for checking all the students' scores.");
printf("\n4 for recomposing the scores.");
printf("\n5 for checking the only one student's score.");
printf("\n6 for exiting.\n");
scanf("%d",&select);
printf("\n********************************************************************************\n");
do
{
switch(select) /* use switch to prompt 3 choice */
{
case 1: /* start adding new records */
add_record(tosp); /* prototype for add_record function */
break;
case 2: /* start deleting new records */
delet_record(tosp); /* prototype for delet_record function */
break;
case 3: /* start checking scores */
check_scores(tosp); /* prototype for check_scores function */
break;
case 4: /* start recomposing the scores */
recompose_scores(tosp);
break;
case 5: /* start checking scores */
check(tosp);
break;
case 6: /* exit the program */
save_record(tosp);
exit(1);
break;
} /* end of switch statement */
printf("\n********************************************************************************");
printf("\nEnter a select code:");
printf("\n1 for adding new records.");
printf("\n2 for deleting new records.");
printf("\n3 for checking scores.");
printf("\n4 for recomposing the scores.");
printf("\n5 for checking the only one student's score.");
printf("\n6 for exiting.\n");
}
while(scanf("%d",&select)!=EOF); /* use do-while let the 5 functions back to the main function */
printf("\n********************************************************************************\n");
}
void add_record(struct Stdinfo *p)
{
char names[MAX];
char num[MAX];
float score;
void push(struct Stdinfo *,char *,char *,float);
getchar();
printf("\n--------------------------------------------------------------------------------\n");
printf("\nEnter as many information as you wish.\n");
while (1)
{
printf("\nPlease enter the student's number:");
printf("\n(To stop entering,enter a single x)");
scanf("%s",num);
getchar();
if (strcmp(num,"x")==0)
break;
printf("Please enter the student's name:");
gets(names);
printf("Please enter the student's score:");
scanf("%f",&score);
getchar();
printf("\nNow you have input the imformation of this student successful.\n");
printf("--------------------------------------------------------------------------------\n");
push(tosp,num,names,score);
}
}
void push(struct Stdinfo *p,char *num,char *names,float score)
{
struct Stdinfo *newaddr;
newaddr=(struct Stdinfo *) malloc(sizeof(struct Stdinfo));
if (newaddr==(struct Stdinfo *) NULL)
{
printf("\nFailed to allocate memory for this structure.\n");
exit(1);
}
strcpy(newaddr->num,num);
strcpy(newaddr->names,names);
newaddr->score=score;
newaddr->nextAddr=p;
tosp=newaddr;
}
void recompose_scores(struct Stdinfo *p)
{
struct Stdinfo *newaddr;
float new_score;
char info[MAX];
getchar();
printf("\nEnter the student's number or name:");
gets(info);
while(1)
{
if(p!=NULL)
{
if(strcmp(p->names,info)==0||strcmp(p->num,info)==0)
{
printf("Enter the new score of this student.");
scanf("%f",&new_score);
p->score=new_score;
break;
}
else
p=p->nextAddr;
}
else
{
printf("\nCan not find this student.\n");
break;
}
}
}
void check_scores(struct Stdinfo *p)
{
char names[MAX];
char num[MAX];
float score;
struct Stdinfo *f,*q,*s;
int i=1;
while(i!=0)
{
q=tosp;
s=q->nextAddr;
i=0;
while(s!=NULL)
{
if(q->score>=s->score)
{
q=s;
s=s->nextAddr;
}
else
{
f->score=s->score;
s->score=q->score;
q->score=f->score;
strcpy(f->names,s->names);
strcpy(s->names,q->names);
strcpy(q->names,f->names);
strcpy(f->num,s->num);
strcpy(s->num,q->num);
strcpy(q->num,f->num);
q=q->nextAddr;
s=q->nextAddr;
i++;
}
}
}
s=tosp;
while(s!=NULL)
{
printf("\n%3s %15s %16.1f\n",s->num,s->names,s->score);
s=s->nextAddr;
}
}
void delet_record(struct Stdinfo *p)
{
struct Stdinfo *newaddr;
char info[MAX];
getchar();
while(1)
{
printf("\nEnter the student's number or name:");
printf("\n(To stop entering,enter a single x)\n");
gets(info);
if (strcmp(info,"x")==0)
break;
while(1)
{
if(p!=NULL)
{
if(strcmp(p->names,info)==0||strcmp(p->num,info)==0)
{
tosp=tosp->nextAddr;
free(p);
break;
}
if(strcmp(p->nextAddr->names,info)==0||strcmp(p->nextAddr->num,info)==0)
{
p->nextAddr=p->nextAddr->nextAddr;
break;
}
else
p=p->nextAddr;
}
else
{
printf("\nCan not find this student.\n");
break;
}
}
}
}
void save_record(struct Stdinfo *tosp)
{
FILE *infile;
infile=fopen("c:\\record.txt","w"); /* to record the information in the "record.txt" file */
if(infile==NULL)
{
printf("\nFailed to open the file.\n");
exit(1);
}
while(tosp!=NULL)
{
fwrite(tosp,sizeof(struct Stdinfo),1,infile);
tosp=tosp->nextAddr;
}
fclose(infile);
}
void check(struct Stdinfo *p)
{
char info[MAX];
struct Stdinfo *newaddr;
getchar();
while(1)
{
printf("\nEnter the student's number or name:");
printf("\n(To stop entering,enter a single x)\n");
gets(info);
if (strcmp(info,"x")==0)
break;
while(1)
{
if(p!=NULL)
{
if(strcmp(p->names,info)==0||strcmp(p->num,info)==0)
{
printf("The student's score is:%3.1f",p->score);
break;
}
else
p=p->nextAddr;
}
else
{
printf("\nCan not find this student.\n");
break;
}
}
}
}