资源描述
#include <stdio.h>
#include <stdlib.h>
#include "malloc.h"
struct node//结构体定义
{int xuehao;
char name[20];
int age;
char sex[3];
struct node *next;//指针域
};
struct node *p,*head,*s,*q;//定义结构体全局变量
struct node *create()//单链表的建立函数
{ int x=1;
head=(struct node*)malloc(sizeof(struct node));//创建头结点,分配空间
head->next=NULL;
p=head;
while(x!=0)
{
s=(struct node*)malloc(sizeof(struct node));//生成新的节点,分配空间
printf("\n请依次输入学生信息:学号,姓名,年龄,性别\n");scanf("%d%s%d%s",&s->xuehao,&s->name,&s->age,&s->sex);//输入添加人员信息
s->next=NULL;
p->next=s;
p=p->next;
printf("如果输入结束,输入0,否则,输入任意");//是否继续添加信息
scanf("%d",&x);
}
printf("创建成功,返回主菜单\n");
return(head);//返回主函数
}
void print(struct node *head)//浏览,打印创建的学生信息函数
{
p=head->next;
while(p!=NULL)
{
printf("学号%4d姓名%4s年龄%4d性别%4s\n",p->xuehao,&p->name,p->age,&p->sex);
p=p->next;
}
printf("这是所有的人员名单,返回主菜单\n");
}
search(struct node *head,int x)//根据学号查询学生信息函数并且不能有相同学号
{
p=head->next;
while(p->xuehao!=x && p->next!=NULL)
p=p->next;
if(p->xuehao!=x)
printf("查无此人!\n");
else
{
printf("这是你要查询的人员信息:\n");
printf("学号%4d\n姓名%4s\n年龄%4d\n性别%4s\n",p->xuehao,&p->name,p->age,&p->sex);//输出查询的学生信息
printf("查询完成,返回主菜单\n");
}
}
insert(struct node *head,int i)//按位置添加插入学生信息函数
{
int j=0;
p=head;
q=p->next;
printf("请输入添加人员信息\n");
s=(struct node*)malloc(sizeof(struct node));
printf("\n请依次输入学生信息:学号,姓名,年龄,性别\n");
scanf("%d%s%d%s",&s->xuehao,&s->name,&s->age,&s->sex);//接收添加人员信息
s->next=NULL;
p=head;q=p->next;
while(q!=NULL&&j<i-1//找到第I个位置
{
p=q;
q=q->next;
++j;
}
if(q!=NULL)
{
s->next=q;
p->next=s;
printf("添加完成,返回主菜单\n");
}
else //当没有查到所要插入的位置时,是否要插入到最后
{
printf("There is no i\n");
printf("*****************************************\n");
printf("是否要插入到最后?\n");
printf("若不再插入到末尾,请输入0,如果插入,请输入其他\n");
scanf("%d",&j);
if(j)
p->next=s;
else
printf("没有添加,返回主菜单\n");
}
}
delet(struct node *head,int x)//删除学生信息函数
{
p=head->next;
q=head;
while(p->xuehao!=x && p!=NULL)
p=p->next;
if(p==NULL) printf("查无此人!无法删除!\n");
else
{
while(q->next!=p)
q=q->next;
q->next=p->next;
free(p);
printf("%d 号学生已删除!\n",x);
}
}
int menu_select()//界面函数
{
int n;
printf("\n 学生信息管理系统\n");
printf("*****************************************\n");
printf(" 1.学生信息线性表的建立\n");
printf(" 2.输 出 所有学生信息\n");
printf(" 3.插 入 学 生 信 息\n");
printf(" 4.查 询 学 生 信 息\n");
printf(" 5.删 除 学 生 信 息\n");
printf(" 0.退 出 管 理 系 统\n");
printf("*****************************************\n");
printf("请选择0-5:\n");
for(;;)
{
scanf("%d",&n);
if (n<0 || n>5)
printf("\n\t输入错误,请重新输入0-5\n");
else
break;
}
return(n);
}
void main()//主函数
{ int i,x;
while(1)//不断循环利用界面函数及其他操作
{
switch(menu_select())//界面函数引用,并且根据返回值确定以后的操作
{
case 1:
{
printf("**************************************\n");
printf(" 学生信息线性表的建立 \n");
create();//建立链表函数引用
printf("————————————————————\n");
}break;
case 2:
{
printf("**************************************\n");
printf("输出所有学生信息\n");
print(head);//打印已创建的学生信息函数的引用
}
break;
case 3:
{
printf("**************************************\n");
printf("添加学生信息\n");
printf("请输入要插入的位置:\n");
scanf("%d",&i);//接收要插入学生位置
insert(head,i); //添加插入学生信息函数的引用
printf("————————————————————\n");
}break;
case 4:
{
printf("**************************************\n");
printf("查询学生信息\n");
printf("您输入要查的学生的学号\n");
printf("-------------------------------------------\n");
scanf("%d",&x);//接收要查询的学生的学号
search(head,x);//根据学号查询学生信息函数的引用
printf("————————————————————\n");
}break;
case 5:
{
printf("**************************************\n");
printf("删除学生信息\n");
printf("您输入要删除的学生的学号\n");
printf("-------------------------------------------\n");
scanf("%d",&x); //接收要删除的学生的学号
printf("-------------------------------------------\n");
delet(head,x); //删除学生信息函数的引用
printf("————————————————————\n");
}break;
case 0:
{printf("再见!\n");break;}
}
}
}
展开阅读全文