简易学生管理系统这里使用的是单链表,简单的实现添加姓名,学号,分数,以及删除,显示添加的信息。
#include<stdio.h>
#include<stdlib.h>
struct node
{
char name[100];
int num;
float grade;
struct node *next;
};
struct node *head;
struct node *last;
void cre_list()
{
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
last = head;
}
void add_node()
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
printf("输入姓名:\n");
scanf("%s",&ptr->name);
printf("输入学号:\n");
scanf("%d",&ptr->num);
printf("输入成绩:\n");
scanf("%f",&ptr->grade);
last->next = ptr;
ptr->next = NULL;
last = ptr;
}
void display_node()
{
struct node *ptr = head->next;
while(ptr != NULL)
{
printf("姓名:%s\t",ptr->name);
printf("学号:%d\t",ptr->num);
printf("成绩:%f\t",ptr->grade);
ptr = ptr->next;
}
}
void find_node()
{
struct node *ptr = head;
char p[100];
printf("输入查找的学生姓名:\n");
scanf("%s",p);
while(strcmp(ptr->name,p)!= 0)
{
ptr = ptr->next;
if(ptr == NULL)
{
printf("不存在此学生\n");
}
printf("姓名:%s\t学号:%d\t成绩:%f\n",ptr->name,ptr->num,ptr->grade);
}
}
void delete_node()
{
struct node *ptr = head;
struct node *str = head;
int i;
printf("输入删除的学号:\n");
scanf("%d",&i);
while(ptr->num != i)
{
str = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("没有此学生\n");
}
}
ptr = str->next;
str->next = ptr->next;
free(ptr);
printf("已经删除\n");
}
void clear()
{
char c;
while((c = getchar()) != '\n' && c != EOF)
{
;
}
}
int main()
{
printf("a:初始\t,b:添加\n");
printf("c:显示\t, d:查找\n");
printf("e:删除\t, f退出");
while(1)
{
printf("please input:\n");
char j;
scanf("%c",&j);
switch(j)
{
case'a':
cre_list();
printf("初始化完成\n");
break;
case 'b':
add_node();
printf("已经添加\n");
break;
case'c':
display_node();
break;
case'd':
find_node();
break;
case'e':
delete_node();
break;
case'f':
exit(0);
default:
break;
}
clear();
}
return 0;
}