C语言学生信息管理系统(源代码)

  • 格式:doc
  • 大小:77.00 KB
  • 文档页数:13

#include

#include

/* 谭法建立单链表和输出单链表 */

#include

#include

typedef struct node

{ int num;

char name[20];

struct node *next;

}STD;

#define LEN sizeof(STD)

int menu_select();

void handle_menu1();

void game4();void game2();

STD *insert(STD *head,STD *stud);STD *insert2(STD *head,STD *stud);

void game3();void game5();

STD *delete(STD*head,long num);

void game1();void game6();

STD *search(STD*head,long num);

STD *head1,*head2;

int n=0;

/*追加法建表 */

void creat1(void)

{ STD *p1,*p2;

system("CLS");

head1=NULL;

p1=(STD *)malloc(LEN);

p2=p1;

printf("建表方法1追加法建表!\n");

printf("输入学号 姓名,若输入学号为0则退出建表!\n");

scanf("%d%s",&p1->num,p1->name);

while(p1->num!=0)

{ n++;

if(n==1)

head1=p1;

else

p2->next=p1;

p2=p1;

p1=(STD *)malloc(LEN);

scanf("%d%s",&p1->num,p1->name);

} free(p1); p2->next=NULL;

scanf("%*c");

printf("按回车键继续!\n");getchar();

system("CLS");

return ;

}

/* 插入法建表 */

void creat2(void)

{ STD *p1;

head2=NULL;

system("CLS");

p1=(STD *)malloc(LEN);

printf("建表方法2插入法建表!\n");

printf("输入学号 姓名(输入学号以0结束)!\n");

scanf("%d%s",&p1->num,p1->name);

while(p1->num!=0)

{ p1->next=head2;

head2=p1;

p1=(STD *)malloc(LEN);

scanf("%d%s",&p1->num,p1->name);

}

scanf("%*c");

printf("按回车键继续!\n");getchar();

system("CLS");

free(p1);

return ;

}

/* 链表输出 */

void print(STD *head)

{ STD *p;

p=head;

while (p!=NULL)

{ printf("学号 %3d 姓名 %s\n",p->num,p->name); p=p->next;}

printf("链表为空!\n");

scanf("%*c");

printf("按回车键继续!\n");getchar();

system("CLS");

}

/*-------------------------------------------------*/ /*--------------表1插入结点函数-------*/

/*-------------------------------------------------*/

void game4()

{

char ch;

STD *stud,*head=head1;system("CLS");

printf("\t仅限在表1中插入!\n");

printf("\t输入要插入结点的学号和姓名(以空格隔开):");

stud=(STD *)malloc(LEN);

scanf("%*c");

scanf("%d%s",&stud->num,&stud->name);

if(stud->num<0)

{ printf("\t输入的学号不能为负数,请重新选择插入!\n");

return;

}

while(stud->num>=0)

{

head1=insert(head,stud);

printf("\t继续插入新结点吗?(y/n):");

ch=getchar();

if(ch=='y'||ch=='Y')

{ printf("\t输入新结点学号 姓名:(输入n结束插入)");

stud=(STD *)malloc(LEN);

scanf("%*c");

scanf("%d%s",&stud->num,&stud->name);

}

else

break;

}

printf("\t输入回车键返回!\n");

getchar();

system("CLS");

printf("\n");

}

/*-------------------------------------------------*/

/*--------------表2插入结点函数-------*/

/*-------------------------------------------------*/

void game2()

{

char ch; STD *stud,*head=head2;system("CLS");

printf("\t仅限在表2中插入!\n");

printf("\t输入要插入结点的学号和姓名(以空格隔开):");

stud=(STD *)malloc(LEN);

scanf("%*c");

scanf("%d%s",&stud->num,&stud->name);

if(stud->num<0)

{ printf("\t输入的学号不能为负数,请重新选择插入!\n");

return;

}

while(stud->num>=0)

{

head2=insert2(head,stud);

printf("\t继续插入新结点吗?(y/n):");

ch=getchar();

if(ch=='y'||ch=='Y')

{ printf("\t输入新结点学号 姓名:(输入n结束插入)");

stud=(STD *)malloc(LEN);

scanf("%*c");

scanf("%d%s",&stud->num,&stud->name);

}

else

break;

}

printf("\t输入回车键返回!\n");

getchar();

system("CLS");

printf("\n");

}

/**************************************/

/* 插入模块 */

/**************************************/

STD *insert(STD *head,STD *stud)

{

STD *p0,*p1,*p2;

p1=head;

p0=stud;

if(head==NULL)

{head=p0;p0->next=NULL;

}

else

{while((p0->num>p1->num)&&(p1->next!=NULL)) {

p2=p1; p1=p1->next;

}

if(p0->num<=p1->num)

{

if(head==p1)head=p0;

else p2->next=p0;

p0->next=p1;

}

else

{

p1->next=p0;p0->next=NULL;

}

}n=n+1;

printf("按回车键继续!\n");getchar();

system("CLS");

return (head);

}

/**************************************/

/* 插入模块 2 */

/**************************************/

STD *insert2(STD *head,STD *stud)

{

STD *p0,*p1,*p2;

p1=head;

p0=stud;

if(head==NULL)

{head=p0;p0->next=NULL;

}

else

{while((p0->numnum)&&(p1->next!=NULL))

{

p2=p1; p1=p1->next;

}

if(p0->num>=p1->num)

{

if(head==p1)head=p0;

else p2->next=p0;

p0->next=p1;

}

else

{