#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int NodeList;
struct Student
{
//学生学号
int id;
//学生姓名
char name[20];
//分数
int grade;
//头指针位置
struct Student *next;
};
//创建链表
/*
struct Student * CreateList(struct Student students[],int n)
{
struct Student *temp;
struct Student *head;
for(int i=0;i<n;i++)
{
struct Student *p=(struct Student *)malloc(sizeof(struct Student));
p->id=students[i].id;
strcpy(p->name,students[i].name);
p->grade=students[i].grade;
p->next=NULL;
if(i==0)
head=p;
else
temp->next=p;
temp=p;
}
return head;
//
}*/
struct Student * CreateNode()
{
struct Student *temp;
struct Student *head;
int ExitType=0;
while(1)
{
struct Student *p=(struct Student *)malloc(sizeof(struct Student));
int ID,Marks;
char s[20];
printf("请输入学号、姓名、成绩:");
scanf("%d%s%d",&ID,&s,&Marks);
p->id=ID;
strcpy(p->name,s);
p->grade=Marks;
p->next=NULL;
if(NodeList==0)
head=p;
else
temp->next=p;
temp=p;
NodeList++;
printf("需要继续输入吗?继续按1,退出按2,其他键默认也是退出!");
scanf("%d",&ExitType);
if(ExitType==2 || ExitType!=1)
break;
}
return head;
}
//遍历 链表
void ForeachNode(struct Student * head)
{
struct Student *p=head;
while(p!=NULL)
{
printf("%d,%s,%d\n",p->id,p->name,p->grade);
p=p->next;
}
}
//主函数开始
void main()
{
//静态数组
/*
struct Student students[3]={{1001,"Tom",90},{1002,"Tom2",90},{1003,"Tom3",90}};
for(int i=0;i<3;i++)
{
printf("%d,%s,%d",students[i].id,students[i].name,students[i].grade);
}
*/
//动态分配
/*
struct Student *p=(struct Student *)malloc(sizeof(struct Student));
p->id=1001;
strcpy(p->name,"Test");
p->grade=95;
*/
//struct Student *p=(struct Student *)malloc(sizeof(struct Student));
//struct Student students[3]={{1001,"Tom",90},{1002,"Tom2",95},{1003,"Tom3",93}};
struct Student *head=CreateNode();
ForeachNode(head);
}