C语言–动态链表菜单–实现建、输、增、删、逆序
第一篇CSDN博客,就先简单点了…
- 对应应用:学生信息管理系统.
- 代码如下:
C语言–动态链表菜单–实现建、输、增、删、逆序。
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
char zy[20];//专业
char name[20];
double xh;//学号
double cj;//成绩
struct student *next;
};
int n;
struct student *CreatList(void)//链表的建立
{
struct student *head,*p1,*p2;
head = NULL; n = 0;
p1 = p2 = (struct student *)malloc(LEN);
scanf("%lf%s%s%lf", &p1->xh, p1->name, p1->zy, &p1->cj);
while(p1->xh!=0)//用于结束输入
{
n++;
if (n == 1)
head = p1;
else p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%lf%s%s%lf", &p1->xh, p1->name, p1->zy, &p1->cj);
}
p2->next = NULL;
free(p1);