您的位置:首页 > 编程语言

无空头的链表代码:学生管理系统

2012-11-23 18:36 459 查看
/*
*程序
*/
//********学员管理
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "he.c"
typedef struct STU{
int num;//学号
char name[20];//姓名
double score;//分数
struct STU *next;
}node,*linklist;
int  node_count=0;
int getlen(linklist head)
{
node *p;
int count=0;
p=head->next;
while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}

int menu()
{
int select;
printf("1,录入学员信息\n");
printf("2,显示学员信息\n");
printf("3,修改学员信息\n");
printf("4,删除学员信息\n");
printf("5, 排序\n");
printf("6,退出\n");
printf("请输入你的选择:");
scanf("%d",&select);
return select;
}

int menu2()
{
int select;
printf("请输入查找依据\n");
printf("1,按编号查找\n");
printf("2,按姓名查找\n");
printf("3,按学号查找\n");
printf("4,返回上级菜单\n");
printf("请输入你的选择:");
scanf("%d",&select);
return select;
}

int menu3()
{
int select;
printf("1,按姓名排序\n");
printf("2,按学号排序\n");
printf("3,按分值排序\n");
printf("4, 返回上级菜单\n");
printf("请输入你的选择:");
scanf("%d",&select);
return select;
}

void input(node *p)
{
printf("\n");
printf("请输入学员学号\n");
scanf("%d",&p->num);
//fflush(stdin);//清空输入缓冲区,清除多余的换行符
printf("请输入学员姓名\n");
scanf("%s",p->name);
printf("请输入学员分数\n");
scanf("%lf",&p->score);
}

void output(node *p)
{
printf("--------------------------------\n");
printf("姓名:%s\n",p->name);
printf("学号:%d\n",p->num);
printf("成绩:%f\n",p->score);
printf("--------------------------------\n");
}

void initiallist(linklist *p)
{
(*p)=(node *)malloc(sizeof(node));
(*p)->next=NULL;
}

int mod(linklist L,int i)
{
node *p;
int j;
if(i<1||i>getlen(L))
{
printf("输入信息非法!\n");
return 0;
}
p=L->next;
for(j=1;j<i;j++)
{
p=p->next;
}

printf("请输入第%d同学的更改信息",i);
input(p);
return 1;
}

int inputlist(linklist L)
{
node *p,*q;
int j;
//static int  node_count=0;
node_count++;
printf("请输入第%d个学生的信息\n",node_count);
p=L;
for(j=1;j<node_count;j++)
{
p=p->next;
}

q=(node *)malloc(sizeof(node));
p->next=q;
p=q;
input(p);
p->next=NULL;

return 0;
}

int del(linklist L,int i)
{
node *p,*q;
int j;
if(i<1||i>getlen(L))
{
printf("输入不合法\n");
return 0;
}
p=L;
q=L->next;
for(j=1;j<i;j++)
{
p=q;
q=q->next;	/*p->next=q->next;	p=q;q=q->next;*/
}
p->next=q->next;
free(q);	/*p->next=NULL;free(p);*/
node_count--;
return 1;
}

int outputlist(linklist L)
{
int count=1;
while(L->next!=NULL)
{
L=L->next;
printf("**************第%d个学生********\n",count);
output(L);
count++;
}
return 0;
}

node *SearchBySerial(linklist L,int num)
{
int	j=1;
node *p;
p=L;
if(num<1||(getlen(L)<num))
{
printf("输入越界!");
return NULL;
}
while(p->next!=NULL&&j<=num)
{
p=p->next;
j++;
}
return p;
}
node *SearchByName(linklist L,char *name[])
{
node *p;
p=L->next;
while(p!=NULL&&strcmp(name,p->name)!=0)
{
p=p->next;
}
return p;
}

node *SearchByID(linklist L,int stuID)
{
int	j=1;
node *p;
p=L->next;
while(p!=NULL&&stuID!=p->num)
{
p=p->next;
j++;
}
return p;
}


/*------------------------------------------------------------------------------
这是我的代码
*------------------------------------------------------------------------------*/
int bubblesort(linklist L)
{
int len,i,j;
node *tmp,*pf,*p,*tail=NULL;
len=getlen(L);
/*pf=L;
p=pf->next;*/
for(i=0;i<len-1;i++)
{
pf=L;
p=pf->next;
for(j=1;j<len-i;j++)
{
if(strcmp(p->name,p->next->name)==1)
{

pf->next=p->next;
p->next=pf->next->next;
pf->next->next=p;
p=pf->next;

/* 也可以这一段 */
/*pf->next=p->next;
tmp=p->next->next;
p->next->next=p;
p->next=tmp;
p=pf->next;*/
}
pf=pf->next;
p=p->next;
}
}
}

int main()
{
linklist head;
int choose=0,choose1=0,choose3=0,del_i,j=0;
int num,stuID;
char name[20];
initiallist(&head);
while(choose != 6)
{
choose=menu();
switch(choose)
{
case 1:
{
inputlist(head);
}
break;
case 2:
{
outputlist(head);
}
break;
case 3:
choose1=0;
while(choose1!=4)
{
choose1=menu2();
j=0;
switch(choose1)
{
case 1:
{
printf("请输入要查找的编号:");
scanf("%d",&num);
if(SearchBySerial(head,num)!=NULL)
{
printf("------------根据编号查找,找到,请修改--------------\n");
output(SearchBySerial(head,num));
mod(head,num);
}
else
printf("按编号未找到!\n");

}
break;
case 2:
{
printf("请输入要查找的姓名:");
scanf("%s",name);
if(SearchByName(head,name)!=NULL)
{
printf("------------根据姓名查找,找到,请修改--------------\n");
output(SearchByName(head,name));
input(SearchByName(head,name));
}
else
printf("按姓名未找到!\n");
}
break;
case 3:
{
printf("请输入要查找的学号:");
scanf("%d",&stuID);
if(SearchByID(head,stuID)!=NULL)
{
printf("------------根据学号查找,找到,请修改--------------\n");
output(SearchByID(head,stuID));
input(SearchByID(head,stuID));
}
else
printf("按姓名未找到!\n");
}
break;
default:
break;
}
}
break;
case 4:
{
printf("输入要删除的学生的编号\n");
scanf("%d",&del_i);
del(head,del_i);

}
case 5:
{
choose3=0;
while(choose3!=4)
{
choose3=menu3();
j=0;
bubblesort(head);
}
}
break;
//......
default:
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: