【数据结构】单链表无头节点

#include "stdlib.h"
#include "stdio.h"
struct List {
    int xs;
    int zs;
    List *next;
};
void InstList(List *&L,int n) {
    L = (List *)malloc(sizeof(List));
    scanf("%d%d",&L->xs,&L->zs);
    L->next = NULL;
    List *p = L;
    for(int i = 0; i < n - 1; i++) {
        p->next = (List *)malloc(sizeof(List));
        p = p->next;
        scanf("%d%d",&p->xs,&p->zs);
        p->next = NULL;
    }
}
List *ListAdd(List *a,List *b) {
    List *c,*t,*Lc;
    if(a->zs > b->zs) {
        Lc = b;
    } else {
        Lc = a;
    }
    while(a && b) {
        if(a->zs > b->zs) {
            c->next = b;
            c = c->next;
            b = b->next;
        } else if(a->zs < b->zs) {
            c->next = a;
            c = c->next;
            a = a->next;
        } else if(a->zs == b->zs) {
            a->xs += b->xs;
            if(0 == a->xs) {
                t = a;
                a = a->next;
                t = b;
                b = b->next;
                free(t);
                free(t);
            } else {
                c->next = a;
                a = a->next;
                b = b->next;
                c = c->next;
            }
        }
    }
    if(!a)c->next = b;
    else if(!b)c->next = a;
    return Lc;
}
int main(void) {
    List *La,*Lb,*Lc;
    int lenla,lenlb;
    scanf("%d",&lenla);
    if(lenla < 1001) {
        InstList(La,lenla);
        scanf("%d",&lenlb);
        if(lenlb < 1001) {
            InstList(Lb,lenlb);
            Lc = ListAdd(La,Lb);
            while(Lc) {
                printf("%d %d\n",Lc->xs,Lc->zs);
                Lc = Lc->next;
            }
        }
    }
    return 0;
}

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...