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

代码:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
#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;
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44859250/article/details/88983180








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。