学生信息管理系统

 

//学生信息管理链表实现

#include"stdio.h"
#include"string.h"
#include"stdlib.h"

struct student
{
 int number;
 char name[20];
 int score;
 struct student *next;
};

/*函数的声明*/
struct student *crease();//新建链表
struct student *insert(struct student *head,struct student *p);//插入操作
struct student *dele(struct student *head,int number);//删除操作
void print(struct student *head);//输出函数
struct student *dele1(struct student *head);

int size=sizeof(struct student);

int main(void)
{
 struct student *head=NULL,*p;
 int choice,number; 
 int flag=1;
 while(flag){ 
  printf("Please choice:\n 1:crease  2:insert  3:delet  4:print 5:dele1 0:Exit !!!\n");
  printf("choice:");
  scanf("%d",&choice);
  switch(choice){
    case 1 :
       head=crease();//函数调用新建链表
       flag=1;
       break;
    case 2 :
             p=(struct student *)malloc(size);
     
             printf("please input number name and score:\n ");
       scanf("%d%s%d",&p->number,p->name,&p->score);
       head=insert(head,p);//插入函数
       flag=1;
       break;
    case 3 :
       printf("Input number:");
       scanf("%d",&number);
       head=dele(head,number);//函数调用删除操作
       flag=1;
       break;
    case 4 :
       print(head);//函数调用输出函数
       flag=1;
       break;
    case 5 :
       head=dele1(head);
       flag=1;
            break;
    case 0 :
       flag=0;
       break;
    default:
       break;     
    }//switch
  if(flag==0)
   printf("Thank you !!!\n");
 }//while
return 0;
}//main

 

 

/*函数定义*/
//新建链表方式1
/*
struct student *crease()
{
 struct student *head,*tail,*p;
 
 int size=sizeof(struct student);

 head=NULL;
 p=(struct student *)malloc(size);
 printf("please input number,name and score:\n");
 scanf("%d%s%d",&p->number,p->name,&p->score);

 while(p->number!=0){

  p->next=NULL;
        if(head==NULL)
   head=p;
  else
   tail->next=p;
   
  tail=p;
  p=(struct student *)malloc(size);
  printf("please input number,name and score:\n");
  scanf("%d%s%d",&p->number,p->name,&p->score);
 }//while
   
 return head;
}

 

//新建链表方式2  (这里的p2指针就相当于上面的tail指针)其实也是一样的
struct student *crease()
{
 
 struct student *head;
 struct student *p1,*p2;

 head=NULL;
 p1=p2=(struct student *)malloc(size);
 printf("Please enter number,name and score:\n");
 scanf("%d%s%d",&p1->number,p1->name,&p1->score);
 while(p1->number!=0)
 { 
  if(head==NULL)
   head=p1;
  else
   p2->next=p1;
  p2=p1;

  p1=(struct student *)malloc(size);
  //printf("Please enter number name and score:\n");
  scanf("%d%s%d",&p1->number,p1->name,&p1->score);
 }
 p2->next=NULL;
 return head;


}

*/


//新建链表3  无序输入有序输出

struct student *crease()
{
 struct student *head;
 struct student *p;
 int number,score;
 char name[20];

 head=NULL;
 printf("please input number,name and score:\n");
 scanf("%d%s%d",&number,name,&score);
 while(number!=0)
 {
  p=(struct student*)malloc(size);
  p->number=number;
  strcpy(p->name,name);
  p->score=score;
  head=insert(head,p);
  scanf("%d%s%d",&number,name,&score);
 }
 return head;
}

 

//插入操作
struct student *insert(struct student *head,struct student *p)
{
 struct student *pr,*pr1,*pr2;
 pr2=head;
 pr=p;
 if(head==NULL)
 {
  head=pr;
  pr->next=NULL;
 }
 else
 {
  while((pr->number>pr2->number)&&(pr2->next!=NULL))
   {
    pr1=pr2;
    pr2=pr2->next;
   }
  if(pr->number<=pr2->number)
  {
   if(pr2==head)
    head=pr;

   else
    pr1->next=pr;
   pr->next=pr2;
  }
  else
  {
   pr2->next=pr;
   pr->next=NULL;
  }
 }

 return head;
}

 

 

 

//删除操作
struct student *dele(struct student *head,int number)
{
 struct student *p1,*p2;
 p1=head;
 if(head==NULL)
  printf("No records!!!\n");

 else
 {

  while((number!=p1->number)&&(p1->next!=NULL))
   {
    p2=p1;
    p1=p1->next;
   }
  if(p1->number==number)
  {
   if(p1==head)
   {
    head=p1->next;
    free(p1);
   }
   else
   {
    p2->next=p1->next;
    free(p1);
   }
 
  }
  else
   printf("No records!!!\n");
 }
 return head;

}

 


//输出函数
void print(struct student *head)
{
 struct student *pr;
 if(head==NULL)
   {
    printf("\n No Records!!! \n");
    //return;
   }
 else
  {
   printf("The student's records are:\n");
   for(pr=head;pr;pr=pr->next)
   printf("%d  %s  %d\n",pr->number,pr->name,pr->score);
  }
}

 


/*
//删除成绩为60分以下的记录1
struct student *dele1(struct student *head)
{
 struct student *p;
 for(p=head;p;p=p->next)
  if(p->score<60)
   head=dele(head,p->number);

 return head;
}
*/


//删除成绩为60分以下的记录2
struct student *dele1(struct student *head)
{
 struct student *p;
 int flag=1;
 while(flag)
 { 
  for(p=head;p;p=p->next)
  {
   if(p->score<60)
   {
    head=dele(head,p->number);
    flag=1;
    break;
   }
  }

  if(p==NULL)
   flag=0;
 }

 return head;
}

 


/*
//删除成绩为60分以下的记录3
struct student *dele1(struct student *head)
{
 struct student *p1,*p2;
 int flag=1;
 p2=head;

 if(head==NULL)
  printf("No recoreds!!!\n");
 else
 {
  while(flag)
  {
   while((p2->number>=60)&&(p2->next!=NULL))
   {
    p1=p2;
    p2=p2->next;
   }
   if(p2->number<60)
   {
    if(p2==head)
    {
     head=p2->next;
     p2=head;
    }
    else
    {
     p1->next=p2->next;
     p2=p1->next;
    }
    flag=1;
   }
   else
   {
    printf("No records!!!\n");
    flag=0;
   }
  }//while
 }//else

 return head;
}

*/

//删除成绩为60分以下的记录4

你可能感兴趣的:(学生信息管理系统)