用户登录
用户注册

分享至

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

  • 作者: 木林森厸
  • 来源: 51数据库
  • 2021-08-18

本文实例为大家分享了c语言单链表实现学生管理系统的具体代码,供大家参考,具体内容如下

代码:

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <malloc.h>
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  %d\n",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  %d\n",head->num,head->name,head->sex,head->age);
 }
 head=head->next;
 }
}

推荐几篇文章:

c++实现简单的图书管理系统

c++实现简单的职工信息管理系统

c++基础学生管理系统

关于管理系统的更多内容请点击《管理系统专题》进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

软件
前端设计
程序设计
Java相关