#include 
"stdafx.h"
#include 
"malloc.h"
#include 
"stdlib.h"
#include 
"stdio.h"
#define NULL 0
#define LEN sizeof(struct student)

struct student
{
    
long num;
    
float score;
    
struct student *next;
}
;

int n;//n為全局變量,指結(jié)點數(shù)

struct student *creat(void)
{
    
struct student *head;
    
struct student *p1,*p2;
    n
=0;
    p1
=p2=(struct student *)malloc(LEN);//開辟結(jié)點,使p1、p2指向它
    scanf("%ld%f",&p1->num,&p1->score);
    head
=NULL;//此時鏈表中無結(jié)點
    while(p1->num!=0)//約定學(xué)生學(xué)號不為0,如果學(xué)號為0,代表鏈表創(chuàng)建結(jié)束
    {
        n
=n+1;
        
if(n==1)//判斷是否是第一個結(jié)點
            head=p1;//head指向首結(jié)點
        else
            p2
->next=p1;//將新結(jié)點的地址賦給p2結(jié)點的next成員
        p2=p1;//p2指向新結(jié)點
        p1=(struct student *)malloc(LEN);
        scanf(
"%ld%f",&p1->num,&p1->score);
    }

    p2
->next=NULL;//鏈表創(chuàng)建過程結(jié)束
    return(head);
}


void print(struct student *head)
{
    
struct student *p;
    printf(
"\nNow,these %d records are:\n",n);
    p
=head;//p指向第一個結(jié)點
    if(head!=NULL)
        
do
        
{
            printf(
"%ld%5.1f\n",p->num,p->score);
            p
=p->next;
        }
while(p!=NULL);
}


struct student *del(struct student *head,long num)
{
    
struct student *p1,*p2;
    
if(head==NULL)//判斷鏈表是否為空
    {
        printf(
"\nlist null!\n");
    }

    
else
    
{
        p1
=head;
        
while(num!=p1->num&&p1->next!=NULL)
        
{
            p1
=p2;
            p1
=p1->next;//p1后移一個結(jié)點
        }

        
if(num==p1->num)
        
{
            
if(p1==head)
                head
=p1->next;
            
else
                p2
->next=p1->next;//讓p1的前一個結(jié)點指向其后一個結(jié)點
            printf("delete:%ld\n",num);
            n
=n-1;
        }

        
else
            printf(
"%ld not been found!",num);//找不到該結(jié)點
    }

    
return(head);
}


struct student *insert(struct student *head,struct student *stud)
{
    
struct student *p0,*p1,*p2;
    p1
=head;//p1指向第一個結(jié)點
    p0=stud;//p0指向要插入的節(jié)點
    if(head==NULL)//判斷鏈表是否為空
    {
        head
=p0;
        p0
->next=NULL;//p0指向的結(jié)點作為頭結(jié)點
    }

    
else
    
{
        
while((p0->num>p1->num)&&(p1->next!=NULL))
        
{
            p2
=p1;
            p1
=p1->next;//p1后移一個結(jié)點
        }

        
if(p0->num<=p1->num)
        
{
            
if(head==p1)//p0是頭結(jié)點
                head=p0;
            
else
                p2
->next=p0;
            p0
->next=p1;
        }

        
else//p0是尾結(jié)點
        {
            p1
->next=p0;
            p0
->next=NULL;
        }

    }

    n
=n+1;//結(jié)點數(shù)加1
    return(head);
}


void main()
{
    
struct student *head,*stu;
    
long del_num;
    printf(
"input records:\n");
    head
=creat();
    print(head);
    printf(
"\ninput the deleted number:");
    scanf(
"%ld",&del_num);
    
while(del_num!=0)
    
{
        head
=del(head,del_num);
        print(head);
        printf(
"\ninput the deleted number:");
        scanf(
"%ld",&del_num);
    }

    printf(
"\ninput the inserted record:");
    stu
=(struct student *)malloc(LEN);
    scanf(
"%ld%f",&stu->num,&stu->score);
    
while(stu->num!=0)
    
{
        head
=insert(head,stu);
        print(head);
        printf(
"\ninput the inserted record:");
        stu
=(struct student *)malloc(LEN);
        scanf(
"%ld%f",&stu->num,&stu->score);
    }

    free(head);
    system(
"Pause");
}