链表合成

一剑行天下 posted @ 2008年11月01日 23:26 in 学习 , 1357 阅读

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
        struct student
{
        long num;
        float score;
        struct student *next;
};
struct student listA,listB;
int n,m=0;
int main()
{
        struct student *ahead,*bhead,*abhead;
        struct student *creat(void);
        struct student *insert(struct student *,struct student *);
        void print(struct student *);
        printf("input list-A\n");
        ahead=creat();
        print(ahead);
        m=m+n;
        printf("input list -B\n");
        bhead=creat();
        print(bhead);
        m=m+n;
        printf("now the new order list are:\n");
        abhead=insert(ahead,bhead);
        print(abhead);

        return 0;
}
struct student *creat()
{
        n=0;
        struct student *p1,*p2,*head;
        p1=p2=(struct student*)malloc(LEN);
        printf("input the score and num of each student\n");
        scanf("%ld,%f",&p1->num,&p1->score);
        head=NULL;
        while(p1->num!=0)
        {
                n=n+1;
                if(n==1)
                        head=p1;
                else
                        p2->next=p1;
                p2=p1;
                p1=(struct student*)malloc(LEN);
                scanf("%ld,%f",&p1->num,&p1->score);
 
        }
        p2->next=NULL;
        return(head);

}
void print(struct student *head)
{
        struct student * p;
        p=head;
        while(p!=NULL)
        {
                printf("%ld %f\n",p->num,p->score);
                p=p->next;
        }
}
struct student * insert(struct student *ah,struct student *bh)
{
        struct student *pa1,*pa2,*pb1,*pb2;
        pb1=pb2=bh;
        while(pb1!=NULL)
        {
                pa1=pa2=ah;

                if(ah==NULL)
                {
                        ah=pb1;
                         pb1=pb1->next;
                         pb2->next=NULL;

                        pb2=pb1;
                }
                else {
                        while(((pb1->num)>(pa1->num))&&(pa1->next!=NULL))
                        {
                                pa2=pa1;
                                pa1=pa1->next;

                        }
                        if((pb1->num)<=(pa1->num))
                        {
                                if(ah==pa1)
                                {
                                        ah=pb1;
                                        pb1=pb1->next;
                                        pb2->next=pa1;
                                        pb2=pb1;

                                }
                                else
                                        pa2->next=pb1;
                                pb1=pb1->next;
                                pb2->next=pa1;
                                pb2=pb1;             
                        }
                        else
                        {
                                pa1->next=pb1;
                                pb1=pb1->next;
                                  pb2->next=NULL;

                                pb2=pb1;
                        }
                }
        }




        return(ah);
}


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter