您的位置:首页 > 教程文章 > 编程开发

C语言单链表实现学生管理系统

:0 :2021-04-28 10:47:37

本文实例为大家分享了C语言单链表实现学生管理系统的具体代码,供大家参考,具体内容如下
代码:
#include
#include
#include
#include
struct Student
{
int num;//学号
char name[20];//名字
char sex[2];
int age;
struct Student *next;
};
void insert(struct Student **head); //插入
void print(struct Student *head); //遍历所有链表
void dele(struct Student **head); //删除 指定内容
void modify(struct Student **head); // 修改内容
void find(struct Student *head); //查找学生信息
int modify_menu();
int main()
{
struct Student *head = NULL;
int x;
do
{
printf("------------------------------------------n");
printf(" 学生管理系统 n");
printf(" n");
printf(" 1 增加学生 2 删除学生 n");
printf(" n");
printf(" 3 修改资料 4 查找学生 n");
printf(" n");
printf(" 5 显示所有学生 0 退出系统 n");
printf(" n");
printf("------------------------------------------n");
printf("请输入你需要使用的功能n");
scanf("%d",&x);
switch(x)
{
case 0 : break;
case 1 :
insert(&head);
break;
case 2 :
dele(&head);
break;
case 3 :
modify(&head);
break;
case 4 :
find(head);
break;
case 5 :
print(head);
break;
default :
printf ("选择错误!!!n");
break;
}
}while(x);
}
void insert(struct Student **head)
{

struct Student *p = (struct Student*)malloc(sizeof(struct Student));
struct Student *stu=NULL;
printf("num:");
scanf("%d",&(p->num));
printf("name:");
scanf("%s",(p->name));
printf("sex:");
scanf("%s",p->sex);
printf("age:");
scanf("%d",&p->age);
p->next=NULL;
if(*head == NULL)
{
*head = p;
}
else
{
stu = *head;
while(stu->next != NULL)
{
stu = stu->next;
}
stu->next = p;
}
}
void print(struct Student *head)
{
printf("学号 名字 性别 年龄 n");
while(head != NULL)
{
printf("%5d %10s %s %dn",head->num,head->name,head->sex,head->age);
head=head->next;
}
}
void dele(struct Student **head)
{
char arr1[20];
struct Student *p1 = NULL;//指向要删除的前一个结点
struct Student *p2 = *head;//指向要删除的结点
printf("请输入要删除的学生n");
scanf("%s",arr1);
while(p2 != NULL)
{
if(p1==NULL&&strcmp(arr1,p2->name)==0)
{
*head = p2->next;
free(p2);
break ;
}
else if(strcmp(arr1,p2->name)==0)
{
p1->next = p2->next;
free(p2);
break ;
}
p1=p2;
p2=p2->next;
}
print(*head);

}
void modify(struct Student **head) //修改
{
char arr[20];
int x = 0;
struct Student *p = *head;
printf("请输入需要修改资料的名字n");
scanf("%s",arr);
while(p!=NULL)
{
if(strcmp(arr,p->name) ==0)
{
printf("请选择修改的内容n");
x = modify_menu();
printf("请输入新的内容n");
switch(x)
{
case 1 :
scanf("%d",&p->num);
break;
case 2 :
scanf("%s",p->name);
break;
case 3 :
scanf("%s",p->sex);
break;
case 4:
scanf("%d",&p->age);
break;
default :
printf ("选择错误!!!n");
break;
}
print(*head);
break ;
}
p=p->next;
}
}
int modify_menu() //修改的菜单
{
int choose = 0;
printf ("-----------------------------------n");
printf ("* 1 学号 2 姓名 *n");
printf ("* 3 性别 4 年龄 *n");
printf ("* 0 取消修改 *n");
printf ("-----------------------------------n");
scanf ("%d", &choose);
return choose;
}
void find(struct Student *head)
{
char arr[20];
printf("请输入学生姓名n");
scanf("%s",arr);
while(head!=NULL)
{
if(strcmp(arr,head->name)==0)
{
printf("学号 名字 性别 年龄 n");
printf("%5d %10s %s %dn",head->num,head->name,head->sex,head->age);
}
head=head->next;
}
}
推荐几篇文章:
C++实现简单的图书管理系统
C++实现简单的职工信息管理系统
C++基础学生管理系统
关于管理系统的更多内容请点击《管理系统专题》进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持无名。

C++ 随机数字以及随机数字加字母生成的案例
C++实现学生信息管理系统

同类资源