简体   繁体   English

在我的C程序中调试我的链接列表很困难

[英]Difficulty debugging my linked-list in my C program

I have this structure containing links to the same structure type (peca). 我有这个结构,其中包含指向相同结构类型(peca)的链接。 The problem is that when I try and use peca->penext in a different function, the debugger shows that the structure peca is not linked to the next or previous. 问题是,当我尝试在其他函数中使用peca-> penext时,调试器显示peca结构未链接到下一个或上一个。 What am I doing wrong? 我究竟做错了什么?

typedef struct peca{
    int index;
    int esquerda;
    int direita;
    int disponibilidade;
    struct peca *penext;
    struct peca *peant;
}PECA;

typedef struct mao{
    int index;
    int tamanho;
    PECA *ppeca;
    struct mao *pnext;
}MAO;

typedef struct jogada{
    int index;
    PECA *ppeca;
    struct jogada *pnext;
}JOGADA;

typedef struct jogo{
    int nr_maos;
    MAO *pmao;
    JOGADA *pjog;
}JOGO;

void init_jogada(JOGADA *pj) {
pj->ppeca = (PECA *) malloc(sizeof(PECA) * 28);
PECA *paux = pj->ppeca;
  for (int i = 0; i < 28; ++i) {
    paux->index = i;
    paux->disponibilidade = 1;
    if (i == 0)
    {
        paux->peant = NULL;
        paux->penext = paux++;
    }
    else if (i == 27)
    {
        paux->peant = paux--;
        paux->penext = NULL;
    }
    else
    {
        paux->penext = paux++;
        paux->peant = paux--;
    }
    paux++;
  }


}

The way you increment pointers to PECA is just cunfusing, that's probably what is breaking your code. 递增指向PECA的指针的方式只是令人困惑,这很可能破坏了代码。 Since I speak Portuguese, I can tell peant means pelast , so I can infer it is a double linked list. 由于我会说葡萄牙语,因此我可以告诉peant意味着pelast ,因此我可以推断出它是一个双链表。

Your double Linked List is absolutely redundant since your pieces (PECA is piece in english) are in a array. 您的双链表绝对是多余的,因为您的片段(PECA是英文片段)位于一个数组中。 Linked lists are used to iterate over a sequence where items are stored sparse on memory, arrays put items next to each other, that's why you can dereference items with a index. 链接列表用于遍历序列,其中将项目稀疏存储在内存中,数组将项目彼此相邻放置,这就是为什么您可以使用索引取消引用项目的原因。

This is what I think you are doing worng: 我认为这是您正在做的事情:

    paux->penext = paux++; 
    /* Here you set the next, why increment the pointer while assigning?
       paux++ changes the value of paux. You might not want that here */

Maybe you are not aware that paux++ or ++paux or paux-- or --paux does change where paux point to. 也许您不知道paux ++或++ paux或paux--或--paux确实会更改 paux指向的位置。

This is your code corrected: 这是您的代码已更正:

  for (int i = 0; i < 28; ++i) {
    paux->index = i;
    paux->disponibilidade = 1;
    if (i == 0)
    {
        paux->peant = NULL;
        paux->penext = paux +1;
    }
    else if (i == 27)
    {
        paux->peant = paux - 1;
        paux->penext = NULL;
    }
    else
    {
        paux->penext = paux + 1;
        paux->peant = paux - 1;
    }
    /* Ok! Now we really want to increment paux, to use in the next iteration */
    paux++;
  }

As a general rule, Ddn't make special cases in the for loop. 通常,在for循环中不要做特殊情况。 Take the code inside if (i==0) and put it before the loop, and the code inside if (i==27) outisde the loop. if (i==0)内部的代码放入循环之前,而if (i==27)内部的代码超出循环。 It makes the loop itself more simple to the reader and avoid problems in case you change the number of iterations and forget to change the conditions. 它使循环本身对读者来说更简单,并且在您更改迭代次数而忘记更改条件的情况下避免了问题。

Your code will look much simpler if you don't modify paux inside the loop. 如果您不修改paux内的paux您的代码将看起来更加简单。 Use the iterator i to access the objects. 使用迭代器i访问对象。 If you really want to use paux to simplify your syntax, either make it the iterator or assign it at the beginning of the loop: 如果您确实想使用paux简化语法,请使其成为迭代器,或者在循环开始时将其分配:

paux = pj->ppeca + i;

Then you can find the next and the previous with paux + 1 and paux - 1 . 然后,您可以找到下一个和上一个,分别为paux + 1paux - 1

But do you really need next/previous pointers? 但是,您真的需要下一个/上一个指针吗? Without seeing the rest of the code, this may sound a silly question to you, but why do you have a structure that resembles a doubly-linked list when your elements are already allocated into an array? 没有看到其余的代码,这对您来说可能是一个愚蠢的问题,但是当您的元素已经分配到数组中时,为什么会有一个类似于双向链表的结构?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM