题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

编译通过,但总是跑飞

#include <stdio.h>

#include <stdlib.h>

typedef struct Colmn{

    unsigned int key;

    unsigned int vlu;

    struct Colmn* next;

}colmn_t;

int InsertIntoChain(colmn_t *ChainClomn,colmn_t *New){

    if(New->key < ChainClomn->next->key){

        New->next = ChainClomn->next;

        ChainClomn->next = New;

        return 0;

    }else if(New->key == ChainClomn->next->key){

        ChainClomn->next->vlu += New->vlu;

        return 0;

    }

    return 1;

}

int main(void) {

    int line_num=0;

    colmn_t *colmn_l,list_head,*temp_head;

    colmn_l = malloc(line_num*sizeof(colmn_t));

    list_head.key = 0;

    list_head.next = colmn_l;

    temp_head = &list_head;

    scanf("%d",&line_num);

    for(int i=0;i<line_num;i++){

        scanf("\n%u %u",&((colmn_l+i)->key),&((colmn_l+i)->vlu));

        (colmn_l+i)->next = 0;

    }

    for(int i=0;i<line_num;i++){

        while(InsertIntoChain(temp_head,&colmn_l[i])){

            temp_head = temp_head->next;

            continue;

        }

        temp_head = &list_head;

    }

    while(temp_head->next != 0){

        printf("%d %d\n",temp_head->next->key,temp_head->next->vlu);

        temp_head = temp_head->next;

    }

}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务