#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef struct Node{
int id;
char name[MAX];
char xy[MAX];
int year;
int month;
int day;
struct Node *next;
}node;
void cd_main()//菜单页面
{
printf("\n\n");
printf("\t--------------------------------\n");
printf("\t 欢迎使用学生信息管理系统 \n");
printf("\t--------------------------------\n");
printf("\t 1.录入学生信息 \n");
printf("\t 2.查询学生信息 \n");
printf("\t 3.修改学生信息 \n");
printf("\t 4.删除学生信息 \n");
printf("\t 5.显示所有学生信息 \n");
printf("\t 6.退出 \n");
printf("\t--------------------------------\n");
printf("\t请选择功能1-6:");
printf("\n\n");
}
void cd_creat(node *d)//学生信息录入函数
{
printf("\t--------------------------------\n");
printf("请输入\n");
printf("%s","学生学号(id):" );
scanf("%d",&d->id);
getchar();
printf("%s","学生姓名:");
fgets(d->name,MAX,stdin);
printf("%s","学生性别:");
fgets(d->xy,MAX,stdin);
printf("%s","出生日期(年):");
scanf("%d",&d->year);
printf("%s","出生日期(月):");
scanf("%d",&d->month);
printf("%s","出生日期(日):");
scanf("%d",&d->day);
printf("\t--------------------------------\n");
printf("\n\n");
}
void node_creat(node *Head)//在创建时通过id直接对链表排序
{
node *p,*pTemp,*pre;
int t=1;
pTemp=(node *)malloc(sizeof(node));
cd_creat(pTemp);
pre=Head;
p=Head->next;
while(p!=NULL){
if(pTemp->id<p->id){
pre->next=pTemp;
pTemp->next=p;
t=0;
}
pre=p;
p=p->next;
}
if(t){
pre->next=pTemp;
pTemp->next=p;
}
}
void node_printf_one(node *p)//打印单个学生信息
{
printf("\t--------------------------------\n");
printf("%s%08d\n","学生学号(id):",p->id );
printf("%s%s","学生姓名:",p->name);
printf("%s%s","学生性别:",p->xy);
printf("%s%8d年\n","出生日期(年):",p->year);
printf("%s%8d月\n","出生日期(月):",p->month);
printf("%s%8d日\n","出生日期(日):",p->day);
printf("\t--------------------------------\n");
printf("\n\n");
}
void node_printf_all(node *head)//打印所有学生信息
{
node *p=head;
while(p!=NULL){
node_printf_one(p);
printf("\t--------------------------------\n");
printf("\n\n");
p=p->next;
}
}
node *node_find(node *head,int id)//查找学生id节点指针函数
{
node *p=head;
while(p->id!=id){
p=p->next;
}
return p;
}
void node_correct(node *head)//修改函数
{
node *p;
int t;
printf("请输入学生id:");
scanf("%d",&t);
printf("\n\n");
p=node_find(head,t);
cd_creat(p);
}
node *node_delect(node *Head,int id)删除函数
{
node *p=Head->next,*pre=Head;
while(p&&p->id!=id){
pre=p;
p=p->next;
}
if(p!=NULL)
pre->next=p->next;
return p;
}
int main()
{
node *head,*Head;//Head指空头节点
Head=(node *)malloc(sizeof(node));
head=(node *)malloc(sizeof(node));
Head->next=head;
head->next=NULL;
int n,n_id,t=1;
while(1){
cd_main();
scanf("%d",&n);
switch (n) {
case 1:
if(t){
cd_creat(head);
//node_printf_one(head);
t=0;
}
else{
node_creat(Head);
//node_printf_one(head);
}
break;
case 2:
printf("请输入学生id:");
scanf("%d",&n_id);
printf("\n\n");
node_printf_one(node_find(head,n_id));
break;
case 3:
node_correct(head);
break;
case 4:
printf("请输入学生id:");
scanf("%d",&n_id);
if(node_delect(Head,n_id)!=NULL)
printf("删除成功\n");
else
printf("无此id\n");
break;
case 5:
node_printf_all(head);
break;
case 6:
exit(0);
break;
}
printf("请输入任意键继续\n");
getchar();
getchar();
}
return 0;
}
网友评论