文档库 最新最全的文档下载
当前位置:文档库 › C语言版-学生信息管理系统

C语言版-学生信息管理系统

#include
#include
#include
#define NULL 0
#define stu student
struct stu
{long num;
char name[20];
char sex;
float cj;
struct stu *next;
};
int n;/*定义N为全局变量*/
struct stu *chuangjian(void)/*创建函数,此函数带回一个指向链表头的指针*/
{struct stu *head;
struct stu *p1,*p2;
n=0;
p1=p2=(struct stu *)malloc(sizeof(struct stu));/*开辟一个新的内存单元*/
printf("请分别输入下列信息,输入0 0 结束!\n\n学号 名字 性别 成绩 \n");
scanf("%ld,%s,%c,%f",&p1->num,&p1->name,&p1->sex,&p1->cj);/*输入学号,名字,性别,成绩*/
head=NULL;/*让head先等于空*/
while(p1->num!=0)
{
n=n+1; /*结点个数加1*/
if(n==1)head=p1;/*如果只有一个结点,那么首结点就是p1*/
else
p2->next=p1;/*否则P2成了首结点P2的next就成了p1*/
p2=p1;/*P2成为了P1,现在的P2也就相当于刚才的首结点的next*/
p1=(struct stu *)malloc(sizeof(struct stu));/*给P1继续开辟新的内存单元*/
scanf("%ld,%s,%c,%f",&p1->num,&p1->name,&p1->sex,&p1->cj);/*继续输入剩余的学生信息,直到p1->num=0结束*/
}
p2->next=NULL;/*p1->num=0了以后P2就成了尾结点*/
return(head);/*返回首结点*/
}

jiemian()
{
int q;
printf("\n\n\n\n\n\n\n\n\t\t\t\t1.创建学生信息\n\t\t\t\t2.显示学生信息\n\t\t\t\t3.删除学生信息\n\t\t\t\t4.插入学生信息\n\t\t\t\t5.退出\n\n");
scanf("%d",&q);
return q;
}
struct stu *cr(struct stu *head,struct stu *stud) /*插入函数*/
{
struct stu *p0,*p1,*p2;
p1=head;/*让P1指向第一个结点*/
p0=stud;/*P0指向要插入的结点*/
if(head==NULL)/*如果首结点为空*/
{head=p0;p0->next=NULL;}/*让P0指向的结点为首结点,p0->next指向的尾结点为空*/
else
{while((p0->num>p1->num)&&(p1->next!=NULL))/*当要插入的结点num大于首结点num,并且首结点的next不为空*/

{p2=p1;/*让p2指向p1刚才指的结点,现在的P2成了刚才的P1*/
p1=p1->next;}/*P1后移一个结点,这会的p1就指向了它的next结点*/
if(p0->num<=p1->num)/*要插入的结点中num小于或者等于首结点中的num,*/
/*如果p0等于head,要插入的结点中num等于首结点中的num,要插入的结点就成为了首结点,它就会插到那会的首结点head之前*/
{if(head==p1)head=p0;
else p2->next=p0;/*插入到p2指向的结点之后,也就是首结点的后面*/
p0->next=p1;}/*p0的next成为了p1,原来p2的next是p1,p0插入到了p2的后面,p1的前面*/
else
{p1->next=p0;p0->next=NULL;}/*p0指向的num是最小的,所以就插到了原来结点的最后(原来P2是第一个结点,P1成了最后的结点)*/
}
n=n+1;/*结点数加1*/

return(head);
}

void print(st

ruct stu *head)/*显示函数*/
{
struct stu *p;
printf("有%d条记录分别是:\n",n);/*把所有的记录显示出来*/
p=head;
if(head!=NULL)/*如果首结点不为空*/
do
{
printf("%ld%s%c%5.1f",p->num,p->name,p->sex,p->cj);/*输出记录*/
p=p->next;/*每一次都输出下一个P的值*/
}
while(p!=NULL);/*直到P的值为0*/
}
struct stu *del(struct stu *head,int num)/*删除函数*/
{
struct stu *p1,*p2;
if(head==NULL){printf("当前为空");goto end;}/*没有列表*/
p1=head;
while(num!=p1->num&&p1->next!=NULL)/*p1不是要找的结点,并且后面还有结点*/
{p2=p1;p1=p1->next;}/*p1后移一个结点,p2的next指向了原来的P1*/
if(num==p1->num)/*找到了*/
{if(p1==head)/*如果P1指向的是首结点,把第2个结点的地址赋予head*/
head=p1->next;
else p2->next=p1->next;/*否则将下一结点地址赋给前一结点地址*/
printf("delete:%d",num);
n=n-1;
}
else printf("%d找不到!",num);/*找不到该结点*/
end:
return(head);
}

void main ()
{
struct stu *head,*dstu;
int dnum,q;
q=jiemian();
getchar();
switch(q)
{
case 1:head=chuangjian();
getchar();//按Enter键清屏
system("cls");//清屏
jiemian();break;
case 2:print(head);break;
case 3:printf("请输入你要删除的学号:");break;
scanf("%d",&dnum);
while(dnum!=0)
{head=del(head,dnum);
print(head);
printf("请输入你要删除的学号:");
scanf("%ld",&dnum);}
case 4:printf("输入要插入的学号");
dstu=(struct stu *)malloc(sizeof(struct stu));
scanf("%d,%s,%c,%f",&dstu->num,&dstu->name,&dstu->sex,&dstu->cj);
while(dstu->num!=0)
{head=cr(head,dstu);
print(head);
printf("输入要插入的学号");
dstu=(struct stu *)malloc(sizeof(struct stu));
scanf("%d,%s,%c,%f",&dstu->num,&dstu->name,&dstu->sex,&dstu->cj);break;
}
case 5:exit(0);break;
}
getchar();
}

相关文档
相关文档 最新文档