【问题标题】:Printing linked list structure C打印链表结构C
【发布时间】:2015-12-18 22:44:40
【问题描述】:

所以,最近我不得不创建一个链表结构,我认为有一个创建它的功能(希望如此),但现在我遇到了将它打印到控制台这样简单的问题。我不知道我创建的结构是否有问题,或者我的打印有问题。如果有人能发现我的代码有什么问题,我将不胜感激:

struct z { int a; struct z *next; };
struct z *head, *node, *next;
int data, x = 1;

int CreateList() {
    printf("Enter 0 to end\n");
    printf("Enter data no. %d: ", x);
    x++;
    scanf("%d", &data);
    if (data == 0) return 0;
    head = (struct z *)malloc(sizeof(struct z));
    if (head == NULL) { printf("Error creating head"); return 0; }
    node = head;
    node->a = data;
    node->next = NULL;
    while (data) {
        next = (struct z *)malloc(sizeof(struct z));
        if (next == NULL) { printf("Error creating next node no. %d", x); return 0;}
        node = next;
        printf("Enter data no. %d: ", x);
        x++;
        scanf("%d", &data);
        node->a = data;
        node->next = NULL;
    }
    return 0;
}


int main() {
    CreateList();
    node = head;
    while (node != NULL) {
        printf("%d ", node->a);
        node = node->next; //<=== crash on this line
    }
    return 0;
}

我的输出始终只是第一个输入的 int,然后它在标记的行上全部崩溃。

【问题讨论】:

  • 输入、输出和预期输出是什么?
  • node=next; --> node-&gt;next=next; node=next; 也许还有其他事情 很大程度上取决于输入 - 未发布。
  • 函数收集 int 数据片段,将它们放入“a”变量并保存到节点,然后创建新的,并重复。我想要的输出是按照放入链表的顺序打印出来的所有数据片段。
  • 发布输入比描述输入更有用。
  • 不要将malloc和朋友的结果投射到C中。

标签: c list printing


【解决方案1】:

您的main 循环使用了错误的变量:

int main(){
    CreateList();
    node = head;
    while (next != NULL) {
        printf("%d ", node->a);
        node = node->next; //<=== crash on this line
    }
    return 0;
}

你应该改用node:

int main(){
    CreateList();
    node = head;
    while (node != NULL) {
        printf("%d ", node->a);
        node = node->next; //<=== crash on this line
    }
    return 0;
}

顺便提一下,headnodenext 应该是局部变量,head 应该由 CreateList() 返回。

CreateList() 实际上并没有正确创建列表:节点在创建时没有链接到列表,只有第一个节点存储在 head 中。

这是一个更正的版本,它返回列表和相应的main 函数:

struct z { int a; struct z *next; };

struct z *CreateList(void) {
    struct z *head, *node, *next;
    int data, x = 1;

    printf("Enter 0 to end\n");
    printf("Enter data no. %d: ", x);
    x++;
    if (scanf("%d", &data) != 1 || data == 0)
        return NULL;
    head = malloc(sizeof(struct z));
    if (head == NULL) {
        printf("Error creating head");
        return NULL;
    }
    node = head;
    node->a = data;
    node->next = NULL;
    for (;;) {
        printf("Enter data no. %d: ", x);
        x++;
        if (scanf("%d", &data) != 1 || data == 0)
            break;
        next = malloc(sizeof(struct z));
        if (next == NULL) {
            printf("Error creating next node no. %d", x - 1);
            return NULL;
        }
        node->next = next;
        node = next;
        node->a = data;
        node->next = NULL;
    }
    return head;
}

int main(void) {
    struct z *head = CreateList();
    struct z *node;
    for (node = head; node != NULL; node = node->next) {
        printf("%d ", node->a);
    }
    printf("\n");
    return 0;
}

【讨论】:

  • 这些是很好的提示,但是返回这些变量或根本不使用函数而只是生成列表的代码不起作用,我仍然像以前一样在同一行发生崩溃。
  • @Thirstix 除了 chux cmets 的修复。
【解决方案2】:

我认为你的问题是全局变量。使它们在函数中,至少是节点和下一个。按需创建这些,以便在您实际添加值时。作为最后一个提示,对于这种情况,do-while 循环会让你的代码看起来比现在更干净,你肯定会有更少的代码重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2017-02-17
    • 2015-08-28
    • 2021-06-21
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    相关资源
    最近更新 更多