链表头地址改变Ç [英] Linked list head address changes C

查看:118
本文介绍了链表头地址改变Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了结构链表,但每次我再添它改变起始地址链接时不知什么原因,但我想的Y读头的地址是第一项。这是我的code:

 结构关卡* TGH = NULL;
结构检查站**链表=安培; TGH;
结构关卡* CP =的malloc(sizeof的(结构关卡));
CHPO = FOPEN(文件名,模式);
如果(CHPO == NULL){
    的printf(找不到文件);
    出口(1);
}其他{
    对于(i = 0; I<线;我++){        的fscanf(CHPO,%C%D%D:%d个\\ n,&安培; CP->辍学,和放大器; CP-> currentPoint,和放大器; CP->的竞争对手,和放大器; CP->小时,和放大器; CP-GT&;分钟);
        CP->接着= NULL;
        如果(* LinkedList的== NULL){
            的printf(ONCE);
            *链表= CP;
        }其他{
            结构检查点*新的= *链表;
            而(新建 - >!下次= NULL){
                新=新建 - >接下来,
            }
            新建 - >接下来= CP;
        }
    }
}

每一个的fscanf时则切换到下一个,任何想法首地址?

的fscanf(CHPO,%C%D%D:数:%d \\ n

这行后头部地址变更; CP->辍学,和放大器; CP-&GT,及放大器; currentPoint,和放大器; CP-GT&;竞争对手&放大器; CP-GT&;小时,和放大器; CP-GT&;分钟);

的结构是这样的:

 结构检查站{
焦炭辍学;
INT currentPoint;
INT竞争对手;
诠释小时;
INT分钟;
结构检查站旁边*;
};


解决方案

这里的问题是,你不分配新的节点,你只有你一遍又一遍修改一个节点。您需要分配循环中的节点。

I created linked list of my structure, but for some reason every time I add another link it changes head address, but I want y head address be first entry. this is my code:

struct checkPoints *tgh = NULL;
struct checkPoints **linkedlist = &tgh;
struct checkPoints *cp = malloc(sizeof (struct checkPoints));
chPo = fopen(fileName, mode);
if (chPo == NULL) {
    printf("Can't find the files.");
    exit(1);
} else {
    for (i = 0; i < lines; i++) {

        fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
        cp->next = NULL;
        if (*linkedlist == NULL) {
            printf("ONCE");
            *linkedlist = cp;
        } else {
            struct checkPoints *new = *linkedlist;
            while (new->next != NULL) {
                new = new->next;
            }
            new->next = cp;
        }
    }
}

every fscanf occurs it changes head address to next, any ideas?

Head address changes after this line: fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);

The structure is this:

struct checkPoints{
char dropOut;
int currentPoint;
int competitor;
int hour;
int minute;
struct checkPoints *next;
};

解决方案

The problem here is that you do not allocate new nodes, you only have one node that you change over and over again. You need to allocate the node inside the loop.

这篇关于链表头地址改变Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆