首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

领头结点有序单链表的合并

2012-06-30 
带头结点有序单链表的合并typedef int Itemtypedef struct node{Item itemstruct node *next}Node,*Lis

带头结点有序单链表的合并

typedef int Item;typedef struct node{        Item item;        struct node *next;}Node,*List;void merge1(List la, List lb, List *lc){        Node *pa,*pb,*pc;        pa=la->next;        pb=lb->next;        pc=(*lc)=la;        while(pa && pb){                if(pa->item <= pb->item){                        pc->next=pa;                        pc=pa;                        pa=pa->next;                }else{                        pc->next=pb;                        pc=pb;                        pb=pb->next;                }        }        pc->next=pa?pa:pb;        free(lb);}/*two non-headnode list merge*/List __merge(List la, List lb){        List lc;        if(la==NULL)                return lb;        if(lb==NULL)                return la;        if(la->item <= lb->item){                lc=la;                lc->next=__merge(la->next,lb);        }else{                lc=lb;                lc->next=__merge(la,lb->next);        }   return lc;}List merge2(List la, List lb){        if(la->next==NULL){                free(la);                return lb;        }        if(lb->next==NULL){                free(lb);                return la;        }        List lc=malloc(sizeof(Node));        lc->next=__merge(la->next,lb->next);        free(la);        free(lb);        return lc;}
?

热点排行