c语言链表的增删改查--简单的学生管理系统
程序员文章站
2022-03-09 08:52:30
...
c语言链表的增删改查–简单的学生管理系统
利用链表写成的学生管理系统,有增删改查基础操作。
注释大都写在代码里
下面展示全部代码,因为英语不是很好,大部分英语语句为拼凑而成。
链表的控制界面都写在user_face这个函数里面
下面是一些运行图片
进入软件时的选择界面
添加节点时的图片
修改节点时的图片
下面是 控制界面的代码。
void user_face()
{
//进入软件第一个界面
enter_software_face();
//开始进入选择界面
SetPos(0,0);
print_face();
char m;
SetPos(70,10);color(9);printf("please choose what things do you want to do?");
SetPos(20,14);color(1);printf("1.creat and add student's message");
SetPos(40,18);color(2);printf("2.add student's message in sure place");
SetPos(60,22);color(3);printf("3.change student's message");
SetPos(80,26);color(4);printf("4.print someone message");
SetPos(100,30);color(5);printf("5.delect student's message");
SetPos(120,34);color(6);printf("6.delect all students' message");
SetPos(140,38);color(7);printf("7.print all students' message");
SetPos(160,42);color(8);printf("0.exit");
color(15);
m = getch();
while(m != '0')
{
switch(m)
{
case '1':
head_add();
break;
case '2':
add_point_in_sure_place();
break;
case '3':
change_student_message();
break;
case '4':
search_student_id();
break;
case '5':
delect_sure_message();
break;
case '6':
delect_allpoint();
break;
case '7':
print_all_student_message();
break;
default:
printf("\nyour enter is wrong!\n");
break;
}
SetPos(0,0);
print_face();
SetPos(70,10);color(9);printf("please choose what things do you want to do?");
SetPos(20,14);color(1);printf("1.creat and add student's message");
SetPos(40,18);color(2);printf("2.add student's message in sure place");
SetPos(60,22);color(3);printf("3.change student's message");
SetPos(80,26);color(4);printf("4.print someone message");
SetPos(100,30);color(5);printf("5.delect student's message");
SetPos(120,34);color(6);printf("6.delect all students' message");
SetPos(140,38);color(7);printf("7.print all students' message");
SetPos(160,42);color(8);printf("0.exit");
color(15);
m = getch();
}
//添加边框
print_face();
SetPos(80,25);color(8);printf("thanks to use our system!");
SetPos(160,40);color(4);printf("creater:tuan zi");
SetPos(160,41);printf("2020.7.9");
SetPos(180,45);color(0);system("pause");
}
因为没有图形库所以用光标函数SetPos();来帮助打印界面,用print_face()函数来打印边框,用color()函数来改变字体的颜色。
user_face中的第一个调用函数是利用链表的头添加来添加节点
user_face中的第二个调用函数是在指定位置添加节点
user_face中的第三个调用函数是通过查询某人id来修改这个人的信息
user_face中的第四个调用函数是通过id查询某人的所有信息
user_face中的第五个调用函数是删除指定id的所有信息
user_face中的第六个调用函数是清空所有链表
user_face中的第七个调用函数是打印所有节点信息
/*----------先把链表的基础操作写完
------------写的是链表的增删改查。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#define NAME_NUM 20
/*-----------------------------定义学生的科目-----------------------*/
struct project{
int stu_chinese;
int stu_math;
int stu_english;
int stu_physics;
int stu_chemistry;
int stu_animal;
};
/*-----------------------------定义学生结构体-----------------------*/
struct stu{
char stu_name[NAME_NUM]; //定义学生姓名
int stu_id; //定义学生的学号
int stu_class; //定义学生所在班级
struct project stu_project; //定义学生学科
struct stu *next; //定义向后的指针
struct stu *pre; //定义向前的指针
};
/*------------------首先为链表创建一个头指针,一个尾指针---------------*/
struct stu *head = NULL;
struct stu *end = NULL;
/*-----------------------------光标位置-----------------------------*/
void SetPos(int x,int y)
{
COORD pos;
HANDLE handle;
pos.X = x;
pos.Y = y;
handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,pos);
}
/*-----------------------------定义一些常用的宏-----------------------*/
#define FACE_x 187 //定义界面的横坐标
#define FACE_y 49 //定义界面的纵坐标
/*----------------------------颜色句柄函数---------------------------*/
int color(int num) //num为每一种颜色代表的数字,数字的范围是0-15
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),num);
return 0;
}
/*--------------------------打印界面函数---------------------------*/
void print_face()
{
SetPos(0,0);
color(12);
int i,j;
for(i = 0 ; i <= FACE_y ; i++ )
{
for(j = 0 ; j <= FACE_x ; j++ )
{
if((i == 0) || (j == 0) || (i == FACE_y) || (j == FACE_x))
printf("*");
else
printf(" ");
}
printf("\n");
}
color(15);
}
/*-----------------------------隐藏光标函数--------------------------*/
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_inof ={1,0}; //第二个值隐藏起来,用来隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_inof);
}
/*---------------------------进入程序界面---------------------------*/
void enter_software_face()
{
print_face();
SetPos(80,40);color(3);printf("welcome to use my software");
color(0);system("pause");
HideCursor();
system("cls");
color(15);
}
/*-----------------------------开始创建节点--------------------------*/
struct stu *creat_point()
{
color(1);
char student_name[20];
struct stu *p = (struct stu *)malloc(sizeof(struct stu));
SetPos(70,13);printf("please enter the student's name:");scanf("%s",&student_name);strcpy(p->stu_name,student_name);
SetPos(70,16);printf("please enter the student's id:");scanf("%d",&p->stu_id);
SetPos(70,19);printf("please enter the student's class:");scanf("%d",&p->stu_class);
SetPos(70,22);printf("please enter the student's chinese score:");scanf("%d",&p->stu_project.stu_chinese);
SetPos(70,25);printf("please enter the student's math score:");scanf("%d",&p->stu_project.stu_math);
SetPos(70,28);printf("please enter the student's english score:");scanf("%d",&p->stu_project.stu_english);
SetPos(70,31);printf("please enter the student's physics score:");scanf("%d",&p->stu_project.stu_physics);
SetPos(70,34);printf("please enter the student's chemistry score:");scanf("%d",&p->stu_project.stu_chemistry);
SetPos(70,37);printf("please enter the student's animal score:");scanf("%d",&p->stu_project.stu_animal);
SetPos(70,38);printf("the student's message enter over!");
color(0);system("pause");color(15);
return p;
}
/*----------------------------创建并添加链表-----------------------------*/
void head_add()
{
//打印边框
SetPos(0,0);
print_face();
//利用头添加为链表添加节点
char m;
if(head == NULL || end == NULL)
{
SetPos(80,10);
color(4);
printf("do you want to start add point?");
m = getch();
if(m == '0')
return;
}
while(m != '0')
{
if(head == NULL || end == NULL)
{
struct stu *p = creat_point();
head = p;
end = p;
end->next = NULL;
}else
{
SetPos(0,0);
print_face();
SetPos(80,10);
color(4);
printf("dou you want to add point continue?");
m = getch();
if(m == '0')
break;
struct stu *p = creat_point();
p->next = head;
head->pre = p;
head = p;
}
}
SetPos(80,25);color(10);printf("all student's message enter over!");
color(0);system("pause");color(15);
}
/*----------------------------打印所有学生的信息-----------------------------*/
void print_all_student_message()
{
//打印边框
print_face();
if(head == NULL) //判断链表是否为空
{
SetPos(80,25);color(4);printf("have no student'smessage!");
color(0);system("pause");color(15);
return;
}
//这个可以用定义光标函数来固定每个属性的位置
SetPos(10,5);color(1);printf("name"); SetPos(30,5);color(2);printf("id"); SetPos(50,5);color(3);printf("class");
SetPos(70,5);color(4);printf("chinese"); SetPos(90,5);color(5);printf("math"); SetPos(110,5);color(6);printf("english");
SetPos(130,5);color(7);printf("physics"); SetPos(150,5);color(8);printf("chemistry"); SetPos(170,5);color(9);printf("anmial");
struct stu *p = head;
int n = 8; //用来定义打印信息的行数
while(p != NULL)
{
SetPos(10,n);color(1);printf("%s",p->stu_name);
SetPos(30,n);color(2);printf("%d",p->stu_id);
SetPos(50,n);color(3);printf("%d",p->stu_class);
SetPos(70,n);color(4);printf("%d",p->stu_project.stu_chinese);
SetPos(90,n);color(5);printf("%d",p->stu_project.stu_math);
SetPos(110,n);color(6);printf("%d",p->stu_project.stu_english);
SetPos(130,n);color(7);printf("%d",p->stu_project.stu_physics);
SetPos(150,n);color(8);printf("%d",p->stu_project.stu_chemistry);
SetPos(170,n);color(9);printf("%d",p->stu_project.stu_animal);
++n;
p = p->next;
}
color(3);SetPos(80,45);printf("all student's message is print over!");
color(0);system("pause");color(15);
}
/*----------------------------根据学号查询学生信息-----------------------------*/
void search_student_id()
{
//定义边框
SetPos(0,0);
print_face();
//判断链表是否为空
if(head == NULL || end == NULL)
{
SetPos(80,25);color(4);printf("have no student's message!");
color(0);system("pause");color(15);
return;
}
//当链表不为空的时候
int search_students_id;
SetPos(80,20);color(5);printf("please enter you want to search student's id:");scanf("%d",&search_students_id);
struct stu *p = head; //创建新的指针用来遍历查询所有节点
while(p != NULL)
{
if(p->stu_id == search_students_id)
break;
p = p->next;
}
if(p == NULL)
{
SetPos(0,0);print_face();
SetPos(80,25);color(4);printf("have no you enter student!");
color(0);system("pause");color(15);
return;
}
//已经找到节点,p节点就是你要找的id所在的节点
//打印学生信息
SetPos(80,25);color(13);printf("name:%s",p->stu_name);
SetPos(80,27);printf("id:%d",p->stu_id);
SetPos(80,29);printf("class:%d",p->stu_class);
SetPos(80,31);printf("chinese:%d",p->stu_project.stu_chinese);
SetPos(80,33);printf("math:%d",p->stu_project.stu_math);
SetPos(80,35);printf("english:%d",p->stu_project.stu_english);
SetPos(80,37);printf("physics:%d",p->stu_project.stu_physics);
SetPos(80,39);printf("chemistry:%d",p->stu_project.stu_chemistry);
SetPos(80,41);printf("animal:%d",p->stu_project.stu_animal);
SetPos(80,45);color(6);printf("%s's message print over!",p->stu_name);
color(0);system("pause");color(15);
}
/*----------------------修改学生信息--------------------*/
//用id查询节点
void change_student_message()
{
//打印边框
SetPos(0,0);
print_face();
//首先还是判断链表是不是为空
if(head == NULL || end == NULL)
{
SetPos(80,20);color(4);printf("have no student's message!");
color(0);system("pause");color(15);
return;
}
//当链表不为空,在链表里查找你需要的id
int change_students_id;
SetPos(70,10);color(14);printf("enter you want to change the student's id!:");scanf("%d",&change_students_id);
struct stu *p = head;
while(p != NULL)
{
if(p->stu_id == change_students_id)
break;
p = p->next;
}
if(p == NULL)
{
SetPos(70,20);color(9);printf("have no this student,or you enter wrong id!");
color(0);system("pause");color(15);
return;
}
//现在跳出循环后的指针指向的就是你所要求的节点
SetPos(70,25);color(13);printf("please enter what message do you want to change!:");
SetPos(16,30);color(1);printf("1.name"); SetPos(32,30);color(2);printf("2.id"); SetPos(48,30);color(3);printf("3.class");
SetPos(64,30);color(4);printf("4.chinese"); SetPos(80,30);color(5);printf("5.math"); SetPos(96,30);color(6);printf("6.english");
SetPos(112,30);color(7);printf("7.physics"); SetPos(128,30);color(8);printf("8.chemistry"); SetPos(144,30);color(9);printf("9.animal");
SetPos(160,30);color(10);printf("0.exit");
char m;
m = getch();
while(m != '0')
{
SetPos(0,0);
print_face();
SetPos(70,35);color(7);
switch(m)
{
//这作用域没有错,但是总是报错
case '1':
{
char changeyourid[20];
printf("enter enter what name do you want to change:");
scanf("%s",&changeyourid);
strcpy(p->stu_name,changeyourid);
break;
}
case '2':
printf("please enter waht id do you want to change:");
scanf("%d",&p->stu_id);
break;
case '3':
printf("please enter what class do you want to change:");
scanf("%d",&p->stu_class);
break;
case '4':
printf("please enter what chinese score do you want to change:");
scanf("%d",&p->stu_project.stu_chinese);
break;
case '5':
printf("please enter what math score do you want to change:");
scanf("%d",&p->stu_project.stu_math);
break;
case '6':
printf("please enter what physics score do you want to change:");
scanf("%d",&p->stu_project.stu_physics);
break;
case '7':
printf("please enter what chemistry score do you want to change:");
scanf("%d",&p->stu_project.stu_chemistry);
break;
case '8':
printf("please enter what animal score do you want to change:");
scanf("%d",&p->stu_project.stu_animal);
break;
}
SetPos(0,0);
print_face();
SetPos(70,25);color(13);printf("please enter what message do you want to change!:");
SetPos(16,30);color(1);printf("1.name"); SetPos(32,30);color(2);printf("2.id"); SetPos(48,30);color(3);printf("3.class");
SetPos(64,30);color(4);printf("4.chinese"); SetPos(80,30);color(5);printf("5.math"); SetPos(96,30);color(6);printf("6.english");
SetPos(112,30);color(7);printf("7.physics"); SetPos(128,30);color(8);printf("8.chemistry"); SetPos(144,30);color(9);printf("9.animal");
SetPos(160,30);color(10);printf("0.exit");
m = getch();
}
SetPos(80,40);color(4);printf("change message achievement!");
color(0);system("pause");color(15);
}
/*----------------------------用来获得链表中节点的个数的函数----------------*/
int getnum_in_mylist_point()
{
//用来判断链表中有没有节点
if(head == NULL || end == NULL)
{
printf("\n **********have no student's message!**********\n");
return 0;
}
//当链表不为空的时候
int num = 0; //用来计算节点的数量
struct stu *p = head;
while(p != NULL)
{
p = p->next;
num++;
}
return num;
}
/*----------------------------在指定位置添加学生信息------------------------*/
void add_point_in_sure_place()
{
//打印边框
SetPos(0,0);
print_face();
//判断链表是不是为空
if(head == NULL || end == NULL)
{
SetPos(80,20);color(4);printf("have no student's message!");
color(0);system("pause");color(15);
return;
}
//当链表不为空的时候
int num;
int list_num = getnum_in_mylist_point(); //用来接收链表中所有的节点数目
SetPos(70,10);color(3);printf("pleas enter what num point do you want to add:");scanf("%d",&num);
//这个时候判断你输入的值是否越界,如果输入越界了,直接退出这个函数
if((num < 1) || (num > (list_num + 1)))
{
SetPos(70,25);color(4);printf("you enter num is wrong!");
color(0);system("pause");color(15);
return;
}
//当输入没有越界的时候
//首先创建一个节点
SetPos(0,0);
print_face();
struct stu *p = creat_point();
//添加节点有三种大情况
if(list_num == 1) //第一大种情况,链表中只有一个节点
{
p->next = head;
p->pre = NULL;
head->pre = p;
head = p;
}else //第二大种情况,链表中有多个节点
{
if(num == 1) //第一种小情况:你所要添加的节点的位置在链表的表头,也就是num=1
{
//开始链接节点
p->next = head;
head->pre = p;
head = p;
}else if(num == list_num+1) //第二种小情况,你所要添加的节点的位置在链表的尾部,也就是num = list_num+1
{
//开始链接节点
end->next = p;
p->pre = end;
p->next = NULL;
end = p;
}else //第三种小情况,你要添加节点的位置即不在链表的头部,也不在链表的尾部
{
//开始链接节点,首先要找到需要的前一个节点
int mynum = 0;
struct stu *t = head;
while(mynum != num)
{
t = t->next;
++mynum;
}
//跳出循环后,现在指针t指向的mynum位置原来的节点,我们得找到mynum这个位置节点的前一个节点q
struct stu *q = t ->pre;
q->next = p;
p->next = t;
t->pre = p;
p->pre = q;
}
}
SetPos(70,40);color(10);printf("add point over!");
color(0);system("pause");color(15);
}
/*----------------------------删除指定id对应的学生信息--------------------*/
void delect_sure_message()
{
//打印边框
SetPos(0,0);print_face();
//判断链表是否为空
if(head == NULL || end == NULL)
{
SetPos(80,25);color(4);printf("have no student's message!");
color(0);system("pause");color(15);
return;
}
//当链表不为空的时候
int students_id;
SetPos(80,20);color(9);printf("please enter what id's message do you want to delect:");scanf("%d",&students_id);
//当只有一个节点的时候
if(head == end)
{
if(students_id != head->stu_id)
{
SetPos(0,0);print_face();
SetPos(80,25);color(4);printf("you enter id is wrong!");
color(0);system("pause");color(15);
return;
}else
{
free(head);
head = NULL;
end = NULL;
}
}
//判断这个节点是否存在,如果存在创建指针找到这个节点
struct stu *p = head;
while(p != NULL)
{
if(p->stu_id == students_id)
break;
p = p->next;
}
if(p == NULL)
{
SetPos(0,0);print_face();
SetPos(80,25);color(4);printf("have no the student's id!");
color(0);system("pause");color(15);
return;
}
//找到节点之后有三种情况
if(p == head) //第一种情况,需要删除的节点是头节点
{
struct stu *z = p->next;
head = z;
z->pre = NULL;
free(p);
}else if(p == end) //第二种情况,需要删除的节点是尾节点
{
struct stu *z = p->pre;
z->next = NULL;
end = z;
free(p);
}else //第三种情况,需要删除的节点既不是头节点也不是尾节点
{
//找到这个节点的前一个节点用q表示,并且找到这个节点的后一个节点用h表示
struct stu *q = p->pre;
struct stu *h = p->next;
q->next = h;
h->pre = q;
free(p); //释放节点,防止出现野指针
}
SetPos(80,35);color(6);printf("delect the point over!");
color(0);system("pause");color(15);
}
/*-------------------清空链表中所有的节点------------------*/
void delect_allpoint()
{
//打印边框
print_face();
//判断链表是否为空
if(head == NULL || end == NULL)
{
SetPos(80,25);color(4);printf("have no student's message!do not to delect !");
color(0);system("pause");color(15);
return;
}
//当链表不为空的时候
struct stu *p = head;
struct stu *q = NULL;
while(p != NULL)
{
q = p->next;
free(p); //清楚链表中所有信息
p = q;
}
head = NULL;
end = NULL;
SetPos(80,25);color(7);printf("all students' message delect over!");
color(0);system("pause");color(15);
}
/*-------------------------操作界面------------------------*/
void user_face()
{
//进入软件第一个界面
enter_software_face();
//开始进入选择界面
SetPos(0,0);
print_face();
char m;
SetPos(70,10);color(9);printf("please choose what things do you want to do?");
SetPos(20,14);color(1);printf("1.creat and add student's message");
SetPos(40,18);color(2);printf("2.add student's message in sure place");
SetPos(60,22);color(3);printf("3.change student's message");
SetPos(80,26);color(4);printf("4.print someone message");
SetPos(100,30);color(5);printf("5.delect student's message");
SetPos(120,34);color(6);printf("6.delect all students' message");
SetPos(140,38);color(7);printf("7.print all students' message");
SetPos(160,42);color(8);printf("0.exit");
color(15);
m = getch();
while(m != '0')
{
switch(m)
{
case '1':
head_add();
break;
case '2':
add_point_in_sure_place();
break;
case '3':
change_student_message();
break;
case '4':
search_student_id();
break;
case '5':
delect_sure_message();
break;
case '6':
delect_allpoint();
break;
case '7':
print_all_student_message();
break;
default:
printf("\nyour enter is wrong!\n");
break;
}
SetPos(0,0);
print_face();
SetPos(70,10);color(9);printf("please choose what things do you want to do?");
SetPos(20,14);color(1);printf("1.creat and add student's message");
SetPos(40,18);color(2);printf("2.add student's message in sure place");
SetPos(60,22);color(3);printf("3.change student's message");
SetPos(80,26);color(4);printf("4.print someone message");
SetPos(100,30);color(5);printf("5.delect student's message");
SetPos(120,34);color(6);printf("6.delect all students' message");
SetPos(140,38);color(7);printf("7.print all students' message");
SetPos(160,42);color(8);printf("0.exit");
color(15);
m = getch();
}
//添加边框
print_face();
SetPos(80,25);color(8);printf("thanks to use our system!");
SetPos(160,40);color(4);printf("creater:tuan zi");
SetPos(160,41);printf("2020.7.9");
SetPos(180,45);color(0);system("pause");
}
int main()
{
user_face();
return 0;
}
上一篇: MySQL表的增删改查