简体   繁体   English

当我在C中创建一个双向链表时,为什么我的节点中的变量数似乎很重要?

[英]Why does the number of variables in my node seem to matter when I make a doubly linked list in C?

I was making a doubly-linked list in C and before it had three variables as its data (two ints and a bool after I threw in typedef int bool) and the two pointers next and prev. 我在C中创建了一个双向链表,在它有三个变量作为其数据之前(两个整数和一个bool在我投入了typedef int bool之后)和两个指针next和prev。 I inserted in the back with a function that roughly said 我在后面插入了一个粗略说明的功能

void insert(list *l, int x, int y, bool z)
{
    node *n = makeNode(x, y, z);
    if(isEmpty(*l))
        l->head = l->tail = NULL;
    else
    {
        l->tail->next = n;
        n->prev = l->tail;
        l->tail = n;
    }
}

And my makeNode function said something like: 我的makeNode函数说:

node *makeNode(int x, int y, bool z)
{
    node *n = malloc(sizeof(n));
    n -> x = x;
    n -> y = y;
    n -> z = z;
    n -> next = NULL;
    n -> prev = NULL;
    return n;
}

So then I print it out with something that looked like: 然后我打印出来的东西看起来像:

void printList(list l)
{
    node *i;
    for(i = list.head; i != NULL; i = i -> next)
        printf("%d %d %d\n", i -> x, i -> y, i -> z);
    printf("\n");
}

That one worked perfectly, but then I had 那个工作完美,但后来我有

void reversePrintList(list l)
{
    node *i;
    for(i = list.tail; i != NULL; i = i -> prev)
        printf("%d %d %d\n", i -> x, i -> y, i -> z);
    printf("\n");
}

And that one segfaulted. 那一个发生了分裂。 After playing around with the code, for some odd reason, while the "next" pointers remained intact every time, the "prev" pointers did not point to the nodes I originally had them pointing to, and apparently they weren't pointing at NULL either. 在玩了代码之后,由于一些奇怪的原因,虽然“下一个”指针每次都保持完整,但“prev”指针并没有指向我最初指向他们的节点,显然他们没有指向NULL无论是。

Furthermore, earlier I was trying to implement it as a circular doubly-linked list and for some reason still the next pointers worked just fine but the prev pointers once again were pointing somewhere strange. 此外,早些时候我试图将它作为一个循环的双向链表实现,并且由于某种原因,下一个指针仍然工作得很好但是上一个指针再一次指向某个奇怪的地方。

But what's really weird about this is that when the nodes only contained one int, everything worked perfectly on both the linear and circular doubly-linked list. 但真正奇怪的是,当节点只包含一个int时,一切都在线性和圆形双向链表上完美运行。 And when I do a few syntactic changes to translate the code to C++, it works perfectly with the bigger node. 当我做一些语法更改以将代码转换为C ++时,它可以与更大的节点完美配合。 I looked all over and there was no code I wrote that was supposed to alter the prev pointers outside of the insert function. 我全神贯注地看到没有我写的代码应该改变插入函数之外的prev指针。 So what is going on to mess up the prev pointers and only the prev pointers when I used a bigger node in C? 那么当我在C中使用更大的节点时,怎么会搞砸prev指针和只有prev指针呢?

Thanks! 谢谢!

Nerd With a Vengeance 书呆子复仇

PS Here are my structs PS这是我的结构

typedef struct node{
    int x;
    int y;
    bool z;
    struct node *next;
    struct node *prev;
} node;

typedef struct list{
    node *head;
    node *tail;
} list;

What is this???? 这是什么????

int i;
for(i = list.tail; i != NULL; i = i -> prev)
  printf("%d %d %d\n", i -> x, i -> y, i -> z);  /* WTF??? */

I would expect something more like: 我希望更像是:

struct mystruct {
  int value;
  struct mystruct *next;
  struct  mystruct *prev;
};
...

  struct mystruct *s;
  ...
  while (s != NULL) {
      printf("value=%d next=%x prev=%x\n", s->value, s->next, s->prev);
      ...

PS: Could you post code for the definitions of "list" and "node"? PS:你能发布“list”和“node”定义的代码吗? They might look promising... 他们看起来很有希望......

node *n = malloc(sizeof(n));

This doesn't make any sense. 这没有任何意义。 You want to allocate enough bytes to hold a node, not a pointer to a node. 您希望分配足够的字节来保存节点,而不是指向节点的指针。 Try: 尝试:

node *n = malloc(sizeof node);

I noticed a small thing in the else part of your insert function. 我注意到插入函数的else部分有一个小东西。 After the line l->tail=n, I think you should have 在l-> tail = n之后,我认为你应该有
l->tail->next = NULL l-> tail-> next = NULL
This is for assigning the last node's next to NULL. 这是为了将最后一个节点分配给NULL。 It seems missing from your code. 你的代码似乎遗漏了。

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

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