这是实现单一链接列表的尝试。
问题在于,当尝试用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("%dn", 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("%dn", traverse->data);
traverse = traverse->next;
}
return 0;
}
谢谢大家!
当您获得指针的大小时,例如sizeof(list)
,然后获得指针的大小,而不是指向的。您应该做sizeof *list
。
下一个问题是:
list = list->next;
list = malloc(sizeof(list));
第一行将list
指向list->next
指向的位置,即NULL
。下一行 reasigns 变量指向一些新分配的内存。您实际上无处链接新节点进入列表。
我建议这样的东西:
list = root;
list->data = 1;
list->next = malloc(sizeof *list);
list = list->next;
list->data = 2;
// etc...
链接不同节点的方式存在问题。仔细查看这部分代码:
list->data = 1;
list->next = NULL;
list = list->next;
list = malloc(sizeof(*list));
list->data = 2;
list->next = NULL;
您应该将新节点分配给上一个节点的下一个。但是当您做list = list->next
时。您的列表变量变为NULL
。相反,您应该这样做:
list->data = 1;
list->next = NULL;
list->next = (node *)malloc(sizeof(list));
list = list->next;
list->data = 3;
list->next = NULL;
list->next = (node *)malloc(sizeof(list));
list = list->next;
list->data = 4;
list->next = NULL;
只是专门为这两个语句重新检查您的代码:
list->next = NULL;
list = list->next;
在这里列表 ->下一步指向null。您指的是List = List-> Next;您的假设在这里不正确。因此,您无法正确获得下一个要素。
首先将内存分配给列表 ->接下来,然后尝试指向那里。理想情况下,这不是我做事的方式。但是,只是为了纠正您的逻辑,我正在编写以下代码行:
list->data = 1;
list->next = malloc(sizeof(*list));
list = list->next;
您必须为每个节点进行此更改。
当然,您的根节点的next
项目始终是NULL
,因为您没有为其分配任何其他值。像root->next = another_node
之类的东西缺少。
有一些可用的好教程可以帮助您进行此实施,例如
- Learn-c.org
- cprogramming.com
您应该用新节点的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("%dn", root->data);
root = root->next;
}
}
测试
1
2
3
4
5