2019-10-26 11:29

# 如何正确创建一个单链表（学生表）？

``````#include "stdio.h"
#include "stdlib.h"

#define OK 1
#define  ERROR 0

typedef int num;
typedef char *str;
typedef struct stu
{
num code;
num number;
str name;
num class;
str department;
struct stu *next;
}*stu;

stu student = NULL;
stu e = NULL;

{
stu p = NULL;
int i;

for (i = 0; i < n; i++)
{
p = (stu)malloc(sizeof(stu));

p->code = (i + 1);
printf("请输入第%d个学生的\n学号\n", (i + 1));
scanf("%d", &p->number);
printf("姓名\n");
scanf("%s", &p->name);
printf("班级\n");
scanf("%d", &p->class);
printf("年级\n");
printf("专业\n");
scanf("%s", &p->department);

}
return OK;
}//头插函数

{
stu p, q;
int i;

for (i = 0; i < n; i++)
{
p = (stu)malloc(sizeof(stu));

p->code = (i + 1);
printf("请输入第%d个学生的\n学号\n", (i + 1));
scanf("%d", &p->number);
printf("姓名\n");
scanf("%s", &p->name);
printf("班级\n");
scanf("%d", &p->class);
printf("年级\n");
printf("专业\n");
scanf("%s", &p->department);

q->next = p;
q = p;
}
q->next = NULL;

return OK;
}//尾插函数

int getelem(stu phead, int n, stu *e)
{
stu p;
int i = 1, j = n;

while (i < j)
{
p = p->next;
++i;
}
if (!p || j > i)
{
return ERROR;
}

(*e)->number = p->number;
(*e)->code = p->code;
(*e)->name = p->name;
(*e)->class = p->class;
(*e)->department = p->department;
(*e)->next = NULL;

return OK;
}//读取

{
stu p, s;
int i, j = n;

p = (stu)malloc(sizeof(stu));
i = 0;
while (p && i < j)
{
p = p->next;
i++;
}
if (!p || j > i)
{
return ERROR;
}
printf("11");
s = (stu)malloc(sizeof(stu));
s->number = (*e)->number;
s->code = (p->code + 1);
s->name = (*e)->name;
s->class = (*e)->class;
s->department = (*e)->department;
s->next = p->next;
p->next = s;
s = s->next;

while (!s)
{
s->code = (s->code + 1);
s = s->next;
}

return OK;
}//插入

{
stu p, r;
int i = 1;

while (p && !(p->number = n))
{
p = p->next;
i++;
}
if (!p)
{
return ERROR;
}

r = p->next;
p->next = r->next;

(*e)->number = r->number;
(*e)->code = r->code;
(*e)->name = r->name;
(*e)->class = r->class;
(*e)->department = r->department;

free(r);

return OK;
}//删除单结点

{
stu p, r;

if (p)
{
return ERROR;
}

while (!p)
{
r = p;
p = p->next;
free(r);
}

}//整表清空

int main()
{
printf("\t=========================================================================================\n");
printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
printf("\t*\t\t\t\t欢迎进入学生学籍管理系统\t\t\t\t*\n");
printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
printf("\t=========================================================================================\n");

do
{
int jud1;
int jud2;
int jud3;
int numberofstu = (int)malloc(sizeof(int));
int number = 0;
int confirm = (int)malloc(sizeof(int));

if (student == NULL)
{
printf("请输入您要执行的操作：(1)创建一个学生表；(0)退出系统\n");
jud1 = (int)malloc(sizeof(int));
scanf("%d", &jud1);
if (jud1 == 1)
{
printf("您是要如何创建学生表呢？(1)倒序(2)顺序\n");
scanf("%d", &jud2);
printf("请输入您的学生总数：\n");
scanf("%d", &numberofstu);
if (jud2 == 1)
{
printf("头插函数运行成功!\n");
}
else if (jud2 == 2)
{
printf("尾插函数运行成功!\n");
}
else
{
printf("操作参数错误，系统崩溃，正在退出---\n");
return ERROR;
}
}
else if (jud1 == 0)
{
printf("正在退出系统，请稍后---\n");
return ERROR;
}
else
{
printf("操作参数错误，系统崩溃，正在退出---\n");
return ERROR;
}
}
else if (student != NULL)
{
e = (stu)malloc(sizeof(stu));
e = NULL;
jud3 = (int)malloc(sizeof(int));

printf("请输入您当前要执行的操作：(1)插入一个新的学生信息；(2)删除一个学生的信息；\n(3)读取一个学生的信息；(4)清空当前学生表;(0)退出系统\n");
scanf("%d", &jud3);

if (jud3 == 1)
{
printf("请输入要插入的位置为第几个：\n");
scanf("%d", &numberofstu);
printf("请输入要插入的学生信息\n");
printf("学号\n");
scanf("%d", &e->number);
printf("姓名\n");
scanf("%s", &e->name);
printf("班级\n");
scanf("%d", &e->class);
printf("年级\n");
printf("专业\n");
scanf("%s", &e->department);

printf("函数运行成功！插入成功！\n");
}
if (jud3 == 2)
{
printf("请输入要删除的学生学号:\n");
scanf("%d", number);

printf("函数运行成功！删除成功！\n");
}
if (jud3 == 3)
{
printf("请输入要读取的学生序号：\n");
scanf("%d", &number);

getelem(student, number, &e);

printf("编号：%d\t学号：%d\t姓名：%s\n\t年级：%d班级：%d\t专业：%s\n", e->code, e->number, e->name, e->grade, e->class, e->department);
}
if (jud3 == 4)
{
printf("您确定要删除学生表吗？（请输入“1”来确认）\n");
scanf("%d", &confirm);
if (confirm == 1)
{
printf("学生表信息清空成功！\n");
}
else
{
printf("看来您没有确认哦\n");
}
}
if (jud3 == 0)
{
printf("正在退出系统，请稍后---\n");
return ERROR;
}
}
} while (1);

}

``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答