#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student{
char Name[20];
char Number[10];
struct Student *next;
};
int Count;
struct Student *Create(){
struct Student *pHead = 0;
struct Student *pEnd,*pNew;
Count = 0;
pEnd = pNew =(struct Student *)malloc(sizeof(struct Student));
printf("请输入学生的姓名和学号:(学号为0时结束输入)\n");
scanf("%s",&pNew->Name);
scanf("%s",&pNew->Number);
while(strcmp("0",pNew->Number)!=0){
Count++;
if(Count==1){
pNew->next = pHead;
pHead=pNew;
pEnd=pNew;
}
else{
pNew->next = 0;
pEnd->next=pNew;
pEnd=pNew;
}
pNew=(struct Student *)malloc(sizeof(struct Student));
scanf("%s",&pNew->Name);
scanf("%s",&pNew->Number);
}
free(pNew);
return pHead;
}
void PrintLink(struct Student *pHead){
struct Student *pTemp;
int Index = 1;
printf("表中有%d个学生\n",Count);
pTemp = pHead;
while(pTemp!=0){
printf("第%d个学生是:\n",Index);
printf("姓名:%s\n",pTemp->Name);
printf("学号:%s\n",pTemp->Number);
pTemp = pTemp->next;
Index++;
}
}
int main(){
struct Student *pHead;
pHead = Create();
PrintLink(pHead);
return 0;
}
运行结果
