C语言链表的问题
简单的生成链表的代码:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int id;
char *name;
char sex;
int age;
};
struct StudentList
{
struct Student *student;
struct StudentList *next;
};
struct Student makeStudent(int id,char *name,char sex,int age)
{
struct Student student;
student.id=id;
student.name=name;
student.sex=sex;
student.age=age;
return student;
}
struct StudentList * addList(struct Student *student,struct StudentList *head)
{
struct StudentList sl;
sl.student=student;
sl.next=head;
head=&sl;
return head;
}
void show(struct StudentList *head)
{
struct Student *sp;
while(head!=NULL)
{
sp=head->student;
if(sp!=NULL)
{
printf("ID:%d\tName:%s\tSex:%c\tAge:%d\n",sp->id,sp->name,sp->sex,sp->age);
}
head=head->next;
}
}
int main()
{
struct Student sd=makeStudent(1,"Tom",'F',20);
struct Student sd1=makeStudent(2,"Tony",'M',22);
struct StudentList *head=NULL;
head=addList(&sd,head);
head=addList(&sd1,head);
printf("%s\n",head->student->name); //1
printf("%s\n",head->student->name); //2
// show(head);
return 0;
}