#include#include#includestruct date{ int year; int month; int day;};struct student { int num; char name[10]; int score[2]; struct date birth; struct student pnext;};int icount;struct student *create(){ struct student *phead=NULL; struct student *pnew,*pend; icount=0; printf("please input the information:\n"); pend=pnew=(struct student)malloc(sizeof(struct student)); scanf("%d",&pnew->num); scanf("%s",pnew->name); scanf("%d,%d",&pnew->score[0],&pnew->score[1]); scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day); while(pnew->num!=0) { icount++; if(icount==1) { pnew->pnext=phead; pend=pnew; phead=pnew; } else { pnew->pnext=NULL; pend->pnext=pnew; pend=pnew; } printf("please input the information:\n"); pend=pnew=(struct student*)malloc(sizeof(struct student)); scanf("%d",&pnew->num); scanf("%s",pnew->name); scanf("%d,%d",&pnew->score[0],&pnew->score[1]); scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day); } free(pnew); return phead;}struct student Insert(struct student *phead){ struct student *ptemp,*pnew; int i=1,n; ptemp=phead; printf("please enter the location you want to insert:\n"); scanf("%d",&n); while((nicount)) { printf("input error,please reinput:\n"); scanf("%d",&n); } while(ipnext; i++; } printf("please input the information:\n"); pnew=(struct student)malloc(sizeof(struct student)); scanf("%d",&pnew->num); scanf("%s",pnew->name); scanf("%d,%d",&pnew->score[0],&pnew->score[1]); scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day); pnew->pnext=ptemp->pnext; ptemp->pnext=pnew; icount++; return phead;}void Print(struct student *phead){ struct student *ptemp; int j=1; ptemp=phead; printf("there are all %d students' information:\n",icount); while(ptemp!=NULL) { printf("this is the NO.%d students' information:\n",j); printf("number:%d\n",ptemp->num); printf("name:%s\n",ptemp->name); printf("Maths:%d,Chinese:%d\n",ptemp->score[0],ptemp->score[1]); printf("birthday--year:%d,month:%d,day:%d\n",ptemp->birth.year,ptemp->birth.month,ptemp->birth.day); ptemp=ptemp->pnext; j++; }}int main(){ struct student *p; p=create(); p=Insert(p); Print(p); return 0;}
可否重新排下代码。。。这样谁看的懂!!
编辑栏 _ </> _ 可以实现代码插入。。。
#include#include#includestruct date{ int year; int month; int day;};struct student { int num; char name[10]; int score[2]; struct date birth; struct student pnext;};int icount;struct student *create(){ struct student *phead=NULL; struct student *pnew,*pend; icount=0; printf("please input the information:\n"); pend=pnew=(struct student)malloc(sizeof(struct student)); scanf("%d",&pnew->num); scanf("%s",pnew->name); scanf("%d,%d",&pnew->score[0],&pnew->score[1]); scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day); while(pnew->num!=0) { icount++; if(icount==1) { pnew->pnext=phead; pend=pnew; phead=pnew; } else { pnew->pnext=NULL; pend->pnext=pnew; pend=pnew; } printf("please input the information:\n"); pend=pnew=(struct student*)malloc(sizeof(struct student)); scanf("%d",&pnew->num); scanf("%s",pnew->name); scanf("%d,%d",&pnew->score[0],&pnew->score[1]); scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day); } free(pnew); return phead;}struct student Insert(struct student *phead){ struct student *ptemp,*pnew; int i=1,n; ptemp=phead; printf("please enter the location you want to insert:\n"); scanf("%d",&n); while((nicount)) { printf("input error,please reinput:\n"); scanf("%d",&n); } while(ipnext; i++; } printf("please input the information:\n"); pnew=(struct student)malloc(sizeof(struct student)); scanf("%d",&pnew->num); scanf("%s",pnew->name); scanf("%d,%d",&pnew->score[0],&pnew->score[1]); scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day); pnew->pnext=ptemp->pnext; ptemp->pnext=pnew; icount++; return phead;}void Print(struct student *phead){ struct student *ptemp; int j=1; ptemp=phead; printf("there are all %d students' information:\n",icount); while(ptemp!=NULL) { printf("this is the NO.%d students' information:\n",j); printf("number:%d\n",ptemp->num); printf("name:%s\n",ptemp->name); printf("Maths:%d,Chinese:%d\n",ptemp->score[0],ptemp->score[1]); printf("birthday--year:%d,month:%d,day:%d\n",ptemp->birth.year,ptemp->birth.month,ptemp->birth.day); ptemp=ptemp->pnext; j++; }}int main(){ struct student *p; p=create(); p=Insert(p); Print(p); return 0;}
struct date
{
int year;
int month;
int day;
};
struct student
{
int num;
char name[10];
int score[2];
struct date birth;
struct student *pnext; //这里是指针。。。
};
int icount;
struct student *create()
{
struct student *phead=NULL;
struct student *pnew,*pend;
icount=0;
printf("please input the information:\n");
pend=pnew=(struct student*)malloc(sizeof(struct student));
memset(pend,0,sizeof(student));
memset(pnew,0,sizeof(student));
scanf("%d",&pnew->num);
scanf("%s",pnew->name);
scanf("%d,%d",&pnew->score[0],&pnew->score[1]);
scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day);
while(pnew->num!=0)
{
icount++;
if(icount==1)
{
pnew->pnext=phead;
pend=pnew;
phead=pnew;
}
else
{
pnew->pnext=NULL;
pend->pnext=pnew;
pend=pnew;
}
printf("please input the information:\n");
pend=pnew=(struct student*)malloc(sizeof(struct student));
memset(pend,0,sizeof(student));
memset(pnew,0,sizeof(student));
scanf("%d",&pnew->num);
scanf("%s",pnew->name);
scanf("%d,%d",&pnew->score[0],&pnew->score[1]);
scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day);
}
free(pnew);
return phead;
}
struct student *Insert(struct student *phead)
{
struct student *ptemp,*pnew;
int i=1,n;
ptemp=phead;
printf("please enter the location you want to insert:\n");
scanf("%d",&n);
while((n<0)||(n>icount))
{
printf("input error,please reinput:\n");
scanf("%d",&n);
}
while(i<n)
{
ptemp=ptemp->pnext;
i++;
}
printf("please input the information:\n");
pnew=(struct student*)malloc(sizeof(struct student));
scanf("%d",&pnew->num);
scanf("%s",pnew->name);
scanf("%d,%d",&pnew->score[0],&pnew->score[1]);
scanf("%d,%d,%d",&pnew->birth.year,&pnew->birth.month,&pnew->birth.day);
pnew->pnext=ptemp->pnext;
ptemp->pnext=pnew;
icount++;
return phead;
}
void Print(struct student *phead)
{
struct student *ptemp;
int j=1;
ptemp=phead;
printf("there are all %d students' information:\n",icount);
while(ptemp!=NULL)
{
printf("this is the NO.%d students' information:\n",j);
printf("number:%d\n",ptemp->num);
printf("name:%s\n",ptemp->name);
printf("Maths:%d,Chinese:%d\n",ptemp->score[0],ptemp->score[1]);
printf("birthday--year:%d,month:%d,day:%d\n",ptemp->birth.year,ptemp->birth.month,ptemp->birth.day);
ptemp=ptemp->pnext;
j++;
}
}
int main()
{
struct student *p;
p=create();
p=Insert(p);
Print(p);
return 0;
}
输入数据应该做一个判断,保证数据输入的正确性。。。