/************************************************************************/
/* 此程序是运用动态内存分配的手段来实现用链表的方式存储学生相关信息的。
链表,是一种自定义数据类型。在使用结构体数组或结构体指针存储复杂数据类型
时需要较大的连续的内存空间,若无需要长度的连续空间会致使操作失败,
并且使用结构体数组或结构体指针时在删除或是插入元素时,资源耗用较大。
链表很好地解决了上述这两个问题。但其缺点是在查询时速度不及前者。 */
/************************************************************************/
#include
#include
#include
#define LEN sizeof(struct Student)
int CreateList(int, struct Student **);
int PrintList(struct Student **);
//定义单向链表结点的数据类型
struct Student
{
char name[20];
int age;
int sex;
struct Student * Next;
};
//主函数中只需定义一个头结点指针即可
int main(void)
{
int n;
puts("请输入学生的个数N:");
scanf("%d", &n);
struct Student * head;
head = NULL;
CreateList(n, &head);//地址传递,head是一个地址变量,要对head本身的值进行修改必须将head的地址进行传递;若传递的是head变量内存储的值,不能达到修改head本身值的目的.
PrintList(&head);//指针地址值传递只是对其存储空间的值进行了一份拷贝,无法改变其本身的值。
free(head);
return 0;
}
//创建链表并实现数据输入
int CreateList(int n, struct Student **head)
{
int i = 1;
struct Student * p3;
struct Student * p2