我制作了一个这样的简单列表:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct node{
int am;
struct node *next;
};
typedef struct node node;
int main(){
int n;
node *head=(node *)malloc(sizeof(node));
node *cur=head;
printf("Give me a number:\n");
scanf(" %d",head->am);
cur=head;
while(1){
printf("Give me a number\n");
scanf(" %d",&n);
if(n==0)
break;
cur->am=n;
cur->next=(node *)malloc(sizeof(node));
cur=cur->next;
cur->next=null;
}
travel(head);
printf("Total nodes available :%d\n",count(head));
system("pause");
return 0;
}
现在,travel应该遍历列表中的每个节点,并显示每个节点中保存的整数。
void travel(node *h){
if(h==NULL)
return;
printf("Received data from node: \t %d\n",h->am);
travel(h->next);
}
现在的问题是,当调用travel时,它不会从第一个节点打印整数。它还会打印另一个“从节点接收的数据:”,然后是一个奇怪的数字。 例如 如果我给1,2,3,4作为输入,这些是结果
Received data from node: 2
Received data from node: 3
Received data from node: 4
Received data from node: 4026432
有什么想法吗?
答案 0 :(得分:4)
现在的问题是,当旅行被召唤时,它不会打印出来 第一个节点的整数
这可以从main()
函数
printf("Give me a number:\n");
scanf(" %d",head->am); //this is wrong use of scanf("%d",&head->am);
cur=head;
while(1){
printf("Give me a number\n");
scanf(" %d",&n);
if(n==0)
break;
cur->am=n;
正如我已经提到你错误扫描但这没关系,因为稍后在while
循环中的代码中你用这种方式替换它......
head->am
head
分配给cur
,因此head->am
和cur->am
现在都是相同的......所以在您第一次分配n
时的while循环中到cur->am
,它会被分配到head->am
。所以这就解释了为什么你永远不会打印第一个节点。<强>解决方案:强>
克服它...在while循环中,在分配cur->am=n
之前尝试:
cur->next=(node *)malloc(sizeof(node));
cur=cur->next;
//then... assign
curr->am=n;
<强>建议:强>
正如有人已经说过使用循环遍历/旅行列表要容易得多(没关系......如果你想以递归方式进行)
这是你如何使用循环:
void travel(node *h)
{
if(h==NULL)
return; //list is empty,consider printing "list empty" :)
while(h!=NULL)
{
printf("Received data from node: \t %d\n",h->am);
h=h->next;
}
}
将所有代码放在一起而不更改travel()
函数,如下所示:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int am;
struct node *next;
};
typedef struct node node;
void travel(node *h);
int main() //I have a habit of returning values from main() :)
{
int n;
node *head=(node *)malloc(sizeof(node));
node *cur=head;
printf("Give me a number:\n");
scanf(" %d",&head->am);
cur=head;
while(1)
{
printf("Give me a number\n");
scanf(" %d",&n);
if(n==0)
break;
cur->next=(node *)malloc(sizeof(node));
cur=cur->next;
cur->am=n; //NOTE:here's the change!
cur->next=NULL;
}
travel(head);
return 0; //just to signify successful compilation
}
void travel(node *h)
{
if(h==NULL)
return;
printf("Received data from node: \t %d\n",h->am);
travel(h->next);
}
示例输入: 5 6 3 1 0
示例输出:
Give me a number:
5
Give me a number
6
Give me a number
3
Give me a number
1
Give me a number
0
Received data from node: 5
Received data from node: 6
Received data from node: 3
Received data from node: 1
答案 1 :(得分:1)
至少存在这些问题:
scanf(" %d",head->am)
错误,因为scanf()
需要预期值的内存位置地址&head->am
。0
之后终止,但在放入任何内容之前最后一个节点。答案 2 :(得分:1)
我建议这样:
int main(void){
int n;
node anchor = {0, NULL};//dummy head
node *head, *cur = &anchor;
while(1){
printf("Give me a number\n");
scanf("%d", &n);
if(n==0)
break;
cur->next = malloc(sizeof(node));
cur = cur->next;
cur->am = n;
cur->next = NULL;
}
head = anchor.next;
travel(head);
printf("Total nodes available :%d\n", count(head));
return 0;
}
答案 3 :(得分:0)
你缺少&amp;在第一次扫描时:
scanf(" %d", &head->am);
但是可以在里面做所有的scanf:
int main(){
node *head=0;
node *cur=0;
node *prev=0;
while(1){
prev = cur;
cur=(node *)malloc(sizeof(node));
cur->next=NULL;
printf("Give me a number\n");
scanf("%d",&cur->am);
if(cur->am==0)
break;
if(head == NULL) head = cur;
if(prev != NULL) prev->next = cur;
}
travel(head);
printf("Total nodes available :%d\n",count(head));
return 0;
}
我希望我在这个SO编辑器中写的没有任何错误。
正如有人所说,你应该释放链表..但这超出范围......
HTH