为什么代码在 while 循环后停止执行?

Why does the code stop executing after the while loop?

我正在做一个双向链表,但我发现当我以相反的顺序打印节点时,代码不起作用。好像被前面的while循环卡住了。

这是代码:

#include <stdio.h>
#include <stdlib.h>

struct LLNode
{
    char name;
    struct LLNode *left;
    struct LLNode *right;
};

struct LLNode * createNode (char data)
{
    struct LLNode *temp; temp = (struct LLNode *)malloc(sizeof(struct LLNode));
    temp->name = data;
    temp->left = NULL;
    temp->right = NULL;
    return(temp);
};

int main()
{
    struct LLNode *curr=NULL;
    struct LLNode *head=curr;

    head = curr = createNode ('A') ;
    printf ("curr->name = %c\n", curr->name) ;

    printf ("head %c tail %c\n",head->name,curr->name) ;

    curr->right = createNode('B');
    printf ("curr->right->name = %c\n", curr->right->name) ;

    curr->right->left = curr,
    printf ("curr->right->left->name = %c\n", curr->right->left->name) ;

    curr = curr->right;
    printf ( "curr = %c\n" , curr->name) ;

    printf ("head %c tail %c\n",head->name,curr->name) ;

    curr->right = createNode('C');
    printf("curr->right->name = %c\n",curr->right->name);

    curr->right->left = curr;
    printf("curr->right->left->name = %c\n", curr->right->left->name);

    curr = curr->right;
    printf("curr->name= %c\n",curr->name);

    printf ("head %c tail %c\n",head->name,curr->name) ;

    printf ("Linked list from head to tail: ");

    while (curr!= NULL)
        {
            // Print nodes from beginning to end.
            printf("%c ", head->name);
            head = head->right;
        }

    printf ("Linked list in reverse: ");

    while (curr!= NULL)
        {
            // Print nodes in reverse order.
            printf("%c ", curr->name);
            curr = curr->left;
        }

    return 0;
}

执行while循环“从头到尾打印节点”后,代码似乎停止了,但我不知道为什么。我试过用break,continue等方法还是不能解决这个问题

有人能帮忙吗?

您的代码中存在一些问题。除了不设置 curr->left 值。我认为你迭代链表的方式; head 在第一次迭代结束时将是 NULL 并且控制不会进入反向打印循环。当您到达 curr->left == NULLcurr->right == NULL 时,您需要在不移动头部的情况下跳出循环,具体取决于您是向前还是向后移动。

我对您的代码进行了一些代码更改,并且能够看到所需的输出:

int main() {
    struct LLNode *curr=NULL;
    struct LLNode *head = curr;
    struct LLNode *prev = NULL;

    // Insert first Element
    head = curr = createNode ('A') ;
    printf ("curr->name = %c\n", curr->name) ;

    // Insert Second Element
    curr->right = createNode('B');
    printf ("curr->right->name = %c\n", curr->right->name) ;

    prev = curr;
    curr = curr->right;
    printf ( "curr = %c\n" , curr->name) ;

    // Insert Third Element
    curr->right = createNode('C');
    curr->left = prev;
    printf("curr->right->name = %c\n",curr->right->name);

    prev = curr;
    curr = curr->right;
    curr->left = prev;
    printf("curr->name= %c\n",curr->name);

    printf ("\nLinked list from head to tail: ");
    while (1) {
        // Print nodes from beginning to end.
        printf("%c ", head->name);
        if (head->right == NULL)
            break;
        head = head->right;
    }

    printf ("\nLinked list in reverse: ");
    while (1) {
        // Print nodes from beginning to end.
        printf("%c ", head->name);
        if (head->left == NULL)
            break;
        head = head->left;
    }

    return 0;
}

当我 运行 这样做时,我能够看到正确打印的列表:

curr->name = A
curr->right->name = B
curr = B
curr->right->name = C
curr->name= C

Linked list from head to tail: A B C 
Linked list in reverse: C B A