文档库 最新最全的文档下载
当前位置:文档库 › 学生成绩管理系统

学生成绩管理系统

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct stud_node
{
int num;
char name[20];
int score;
struct stud_node *next;
};

struct stud_node *Create_Stu();
struct stud_node *Insert(struct stud_node *head , struct stud_node *stud);
struct stud_node *Delete(struct stud_node *head,int num);
void Print_Stu(struct stud_node *head);


int main (void)
{
struct stud_node *head,*p;
int num,score,key;
char name[20];
int size=sizeof(struct stud_node*);

head=NULL;
do
{

clrscr();
printf("************************************************\n");
printf("* please select key: *\n");
printf("* 1 Create *\n");
printf("* 2 Insert *\n");
printf("* 3 Dlete *\n");
printf("* 4 Print *\n");
printf("* 0 Exit *\n");
printf("************************************************\n");
scanf("%d",&key);
switch(key)
{
case 1:
clrscr();
head=Create_Stu();
break;
case 2:
clrscr();
printf("please input number name and score:\n");
scanf("%d%s%d",&num,name,&score);
p=(struct stud_node *)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
head=Insert(head,p);
break;
case 3:
clrscr();
printf("Input The Number:\n");
scanf("%d",&num);
head=Delete(head,num);
break;
case 4:
clrscr();
Print_Stu(head);
break;
case 0:
break;
defaut:
break;
}
}
while(key!=0);

return 0;
}

/*新建链表*/
struct stud_node *Create_Stu()
{
struct stud_node *head,*p;
int num,score;
char name[20];
int size=sizeof(struct stud_node);

head=NULL;
printf("Input number:\n");
scanf("%d",&num);
while(num!=0)
{
printf("Input name:\n");
scanf("%s",name);
printf("Input score:\n");
scanf("%d",&score);
p=(struct stud_node *)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
p->next=NULL;
head=Insert(head,p);
printf("Input number:\n");
scanf("%d",&num);

}
return head;
}


/*插入操作*/
struct stud_node *Insert(struct stud_node *head , struct stud_node *stud)
{
struct stud_node *ptr,*ptr1,*ptr2;

ptr2=head;
ptr=stud;
if(head==NULL) /*原链表为空时插入*/
{
head=ptr;
}
else /*原链表不为空时插入*/
{
while((ptr->num>ptr2->num)&&(ptr2->num!=NULL))
{
ptr1=ptr2; /*ptr1,ptr2各后移一个结点*/
ptr2=ptr2->next;
}
if(ptr->num<=ptr2->num) /*在ptr1与ptr2之间插入*/
{
if(head==ptr2)
head=ptr;
else
ptr1->next=ptr;
ptr->next=ptr2;
}
else /*新结点成为末结点*/
{
ptr2->next=ptr;
}
}
return head;
}


/*删除操作*/
struct stud_node *Delete(struct stud_node *head,int num)
{
struct stud_node *p,*q;

while(head!=NULL&&head->num==num) /*删除结点为表头*/
{
q=head;
head=head->next;
free(q);
}
if(head==NULL) /*链表为空*/
return NULL;

p=head; /*删除结点为非表头*/
q=head->next;
while(q!=NULL)
{ /*q所指的结点符合要求*/
if(q->num==num)
{
p->next=q->next;
free(q);
}
else
p=q; /*p后移一个结点*/
q=p->next; /*q指向p的下一个结点*/
}
return head;
}

/*遍历操作*/
void Print_Stu(struct stud_node *head)
{
struct stud_node *p;

if(head==NULL)
{
printf("\n No RecordS !!\n");
printf("\n Press any key to continue...\n");
getch();
return;

}
printf("The student's records:\n");
for(p=head;p;p=p->next)
{
printf("Number:%d\n",p->num);
printf("Name:%s\n",p->name);
printf("Score:%d\n",p->score);
printf("\n");
}
printf("\n Press any key to continue...\n");
getch();
}

相关文档