那个是无名指:C语言当中链表要怎么下移呢?

来源:百度文库 编辑:高考问答 时间:2024/04/28 15:00:23
struct student *head;
struct student *Create();
struct student
{
char sno[NUMLEN];
char name[NAMELEN];
int age;
int score[5];
struct student *next;
};
main()
{
BuildWindow();
Create();
Manu();
}
struct student *Create()
{ struct student *fore=NULL,*new;
new=(struct student *)malloc(LEN);
if(new==NULL)
{ MessageBox("Sorry!Can not malloc the space now!\n\rThe system will be aborted!",0);
exit(0);
}
else
{ if(head==NULL)
head=new;
else
fore->next=new;
fore=new; }
fore->next=NULL;
return head;
}
void AddStuInfo()
{ struct student *head=NULL,*fore,*new,*p;
int label=0,i,inputL;
char temp[23],age[5],name[23];
int age1;
age[0]=3;
name[0]=NAMELEN;
if(inputL==6)/*input Y or y*/
Manu();
else
{
Add: BuildMainWindow();
BuildPrintWin();
AddPrintChoice();
BuildInputWin(Place);
fore=head;
while(fore->next!=NULL)
{
fore=fore->next;
}
fore=Create();
AddNo(fore->sno);
cprintf("%s",fore->sno);
AddMessage();
Place++;
/* ---input the name--- */
BuildInputWin(Place);
do
{
temp[0]=21;
cgets(temp);
label=AddName(&temp[2]);
if(label==1)
{
MessageBox("Sorry!You have input nothing or all space.\n\rPlease input again!",0);
BuildInputWin(Place);
}
else if(label==0)
{
MessageBox("Sorry!You have to input the character or space!\n\rPlease input again!",0);
BuildInputWin(Place);
}
else
{
strcpy(name,&temp[2]);
break;
}
}while(1);
BuildInputWin(Place);
cputs(name);
strcpy(fore->name,name);
AddMessage(Place);
Place++;
/* ---input the age--- */
BuildInputWin(Place);
do
{
cgets(age);
age1=AddAge(&age[2]);
if(age1==0)
{
MessageBox("Sorry!You should input the number.\n\rPlease input again!",0);
BuildInputWin(Place);
}
else
break;
}while(1);
fore->age=age1;
AddMessage(Place);
Place++;
for(i=0;i<5;i++)
{
fore->score[i]=StoreScore();
AddMessage(Place);
Place++;
}
Place=1;
Count++;
State=1;
inputL=MessageBox("You have inputed a student\'s informatio.\n\rDo you want to continue?\n\rInput \'M\' or \'m\'to manu,\'s\' or '\S\' to save the data .Anykey back to add.",1);
if(inputL==1) /*input M or m*/
Manu();
else if(inputL==3) /*input S or s*/
SaveData();
else
goto Add;
}
}
之前发了一个提问了,但是没有把关键的代码贴出来补充又太多,所以就重新发一个,问题还是一样的,就是只记录了最后一条记录,也可能是添加的时候出现了问题,但是用断点调过了记录是有添加进去的。请各位达人帮忙解开疑惑!谢谢,回答好的可以追加分数
void PrintData(int i)
{ struct student *fore;
int score=0,j,k;
float avg=0.0;
do
{ fore=head;
for(j=0;j<5;j++)
{ score+=fore->score[j];
avg=score/5.0;
} gotoxy(LEFT-1,1+i%MAXPRINT);
cprintf("%s",fore->name);
gotoxy(LEFT+21,1+i%MAXPRINT);
cprintf("%s",fore->sno);
gotoxy(LEFT+26,1+i%MAXPRINT);
cprintf("%d",fore->age);
gotoxy(LEFT+32,1+i%MAXPRINT);
cprintf("%d",fore->score[0]);
gotoxy(LEFT+38,1+i%MAXPRINT);
cprintf("%d",fore->score[1]);
gotoxy(LEFT+45,1+i%MAXPRINT);
cprintf("%d",fore->score[2]);
gotoxy(LEFT+52,1+i%MAXPRINT);
cprintf("%d",fore->score[3]);
gotoxy(LEFT+57,1+i%MAXPRINT);
cprintf("%d",fore->score[4]);
gotoxy(LEFT+62,1+i%MAXPRINT);
cprintf("%d",score);
gotoxy(LEFT+71,1+i%MAXPRINT);
cprintf("%.1f",avg);
fore=fore->next;
}while(fore!=NULL);
} 我想通过这段代码把所录入的信息显示出来,但是因为我的指针没有处理好,总是显示最后录入的那条信息,但是循环也会走那么多次。我想请问一下怎么处理才能从第一次录入的信息开始打印。谢谢

好 给你看看
struct student *head; //struct student *head=NULL
struct student *Create();

.............
.............
struct student *Create()
{ struct student *fore=NULL,*new;
new=(struct student *)malloc(LEN);
if(new==NULL)
{ MessageBox("Sorry!Can not malloc the space now!\n\rThe system will be aborted!",0);
exit(0);
}
else
{ if(head==NULL)
head=new;
else
fore->next=new;
fore=new; }
fore->next=NULL;
return head;
}
void AddStuInfo()
{ struct student *head=NULL,*fore,*new,*p;
int label=0,i,inputL;
char temp[23],age[5],name[23];
int age1;
age[0]=3;
name[0]=NAMELEN;
if(inputL==6)/*input Y or y*/
Manu();
else
{
Add: BuildMainWindow();
BuildPrintWin();
AddPrintChoice();
BuildInputWin(Place);
fore=head;
while(fore->next!=NULL)
{
fore=fore->next;
}
fore=Create();
AddNo(fore->sno);
cprintf("%s",fore->sno);
AddMessage();
Place++;
/* ---input the name--- */
BuildInputWin(Place);
do
{
temp[0]=21;
cgets(temp);
label=AddName(&temp[2]);
if(label==1)
{
MessageBox("Sorry!You have input nothing or all space.\n\rPlease input again!",0);
BuildInputWin(Place);
}
else if(label==0)
{
MessageBox("Sorry!You have to input the character or space!\n\rPlease input again!",0);
BuildInputWin(Place);
}
else
{
strcpy(name,&temp[2]);
break;
}
}while(1);
BuildInputWin(Place);
cputs(name);
strcpy(fore->name,name);
AddMessage(Place);
Place++;
/* ---input the age--- */
BuildInputWin(Place);
do
{
cgets(age);
age1=AddAge(&age[2]);
if(age1==0)
{
MessageBox("Sorry!You should input the number.\n\rPlease input again!",0);
BuildInputWin(Place);
}
else
break;
}while(1);
fore->age=age1;
AddMessage(Place);
Place++;
for(i=0;i<5;i++)
{
fore->score[i]=StoreScore();
AddMessage(Place);
Place++;
}
Place=1;
Count++;
State=1;
inputL=MessageBox("You have inputed a student\'s informatio.\n\rDo you want to continue?\n\rInput \'M\' or \'m\'to manu,\'s\' or '\S\' to save the data .Anykey back to add.",1);
if(inputL==1) /*input M or m*/
Manu();
else if(inputL==3) /*input S or s*/
SaveData();
else
goto Add;
}
}

帮你做下看看

恩,有点难