gpt4 book ai didi

c - 奇怪的数字出现在输出-C

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:18 24 4
gpt4 key购买 nike

我做了一个像这样的简单列表:

#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;
}

现在旅行应该遍历列表中的每个节点并显示每个节点中保存的整数。

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

有什么想法吗?

最佳答案

Now the problem is that when travel is called it wont print the integer from the first node

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->amcur->am 都是现在相同...所以在 while 循环中,当您第一次将 n 分配给 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

关于c - 奇怪的数字出现在输出-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37748106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com