我的单链表实施有什么问题?

时间:2017-06-26 05:39:13

标签: c linked-list singly-linked-list

这是尝试实施单链表 问题是当尝试使用while (traverse != NULL)打印列表时,程序输出1,第一个节点的数据,但不打印所有其他节点的数据。我是否错误地链接了节点,如果是,那么在哪里?

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

typedef struct node {
    int data;
    struct node *next;
} Node;

struct node *root;

int main(void) {
    Node *list, *traverse;
    /* root will always be the first of the list */
    root = malloc(sizeof(*list));

    list = root;

    list->data = 1;
    list->next = NULL;
    list = list->next;

    list = malloc(sizeof(*list));
    list->data = 2;
    list->next = NULL;
    list = list->next;

    list = malloc(sizeof(*list));
    list->data = 3;
    list->next = NULL;
    list = list->next;

    list = malloc(sizeof(*list));
    list->data = 4;
    list->next = NULL;
    list = list->next;

    list = malloc(sizeof(*list));
    list->data = 5;
    list->next = NULL;
    list = list->next;

    traverse = root;

    while (traverse != NULL) {
        printf("%d\n", traverse->data);
        traverse = traverse->next;
    }   
    return 0;
}

输出:

$ gcc main.c && ./a.out
1


预期产出:

$ gcc main.c && ./a.out
1
2
3
4
5

更新: 我已经更新了我的源文件,正如你们所有人都建议的那样:

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

typedef struct node {
    int data;
    struct node *next;
} Node;

struct node *root;

int main(void) {
    Node *list, *traverse;
    /* root will always be the first of the list */
    root = malloc(sizeof(*list));

    list = root;

    list->data = 1;
    list->next = malloc(sizeof(*list));

    list = list->next;
    list->data = 2;
    list->next = malloc(sizeof(*list));

    list = list->next;
    list->data = 3;
    list->next = malloc(sizeof(*list));

    list = list->next;
    list->data = 4;
    list->next = malloc(sizeof(*list));

    list = list->next;
    list->data = 5;
    list->next = NULL;

    traverse = root;

    while (traverse != NULL) {
        printf("%d\n", traverse->data);
        traverse = traverse->next;
    }   
    return 0;
}

非常感谢大家!

5 个答案:

答案 0 :(得分:1)

当你得到指针的大小时,例如sizeof(list),然后你得到指针的大小而不是它指向的大小。你应该改为sizeof *list

下一个问题是:

list = list->next;

list = malloc(sizeof(list));

第一行使list指向list->next指向的位置,即NULL。下一行重新分配变量以指向一些新分配的内存。您实际上新节点链接到列表中。

我建议这样的事情:

list = root;

list->data = 1;
list->next = malloc(sizeof *list);
list = list->next;

list->data = 2;
// etc...

答案 1 :(得分:1)

您链接不同节点的方式存在问题。仔细查看这段代码:

   $survey_detail=$survey->viewSurvey('3');
        $questions=$survey->indexQuestion('3');

        while ($question_header = $header_questions->fetch()){
            while ($question_detail = $questions->fetch()){
                if($question_detail['header']==$question_header['header']){
                echo $question_detail['header'] . "-".$question_detail['question']. "<br>";
                }
            }
        }

您应该将新节点分配给下一个节点。但是当你做list->data = 1; list->next = NULL; list = list->next; list = malloc(sizeof(*list)); list->data = 2; list->next = NULL; 时。您的列表变量变为list = list->next。相反,你应该这样做:

NULL

答案 2 :(得分:1)

只需专门针对这两个陈述重新检查您的代码:

list->next = NULL; list = list->next;

这里list-&gt; next指向NULL。你指向list = list-&gt; next;你的假设在这里不正确。因此,你没有正确地获得你的下一个元素。

首先为list-&gt; next分配内存,然后尝试指向那里。理想情况下,这不是我做事的方式。但是为了纠正你的逻辑,我正在写下面的代码行:

list->data = 1;
list->next = malloc(sizeof(*list));
list = list->next;

您必须为每个节点进行此更改。

答案 3 :(得分:0)

当然,根节点的choosing_options = ["Yes","No"] Answer = raw_input("Do you want to become Better?") if Answer == 'Yes' : print 'Great We Will Start Tommorow, meet me at Jhon\'s backyard at 3 AM ' elif Answer == 'No' : print "Well too bad, meet me again if you change your mind " elif Answer != choosing_options : print "You can only choose yes or no!!" 项始终为next,因为您没有为其分配任何其他值。缺少像NULL这样的东西。

有一些很好的教程可以帮助您实现这种实现,例如

  1. learn-c.org
  2. cprogramming.com

答案 4 :(得分:0)

您应该为新节点分配malloc内存:

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

struct node {
    int data;
    struct node *next
};

int main(void) {
    struct node *root, *list;
    int i;
    root = malloc(sizeof(struct node));
    list = root;
    root->next = NULL;

    list->data = 1;
    list->next = malloc(sizeof(struct node));
    list = list->next;

    list->data = 2;
    list->next = malloc(sizeof(struct node));
    list = list->next;

    list->data = 3;
    list->next = malloc(sizeof(struct node));
    list = list->next;

    list->data = 4;
    list->next = malloc(sizeof(struct node));
    list = list->next;

    list->data = 5;
    list->next = malloc(sizeof(struct node));
    list = list->next;

    list->next = NULL;
    while (root->next != NULL) {
        printf("%d\n", root->data);
        root = root->next;
    }
}

测试

1
2
3
4
5