[英]i am trying to read and print one value in a linked list , but my program does not give any output
i am trying to read and print one value in a linked list , but my program does not give any output, i have tryed checking where the program is failing to execute , after the first scanf the code is not printing anything, what might be the reason for that?我正在尝试读取并打印链表中的一个值,但我的程序没有给出任何输出,我尝试检查程序执行失败的位置,在第一次扫描后代码没有打印任何内容,可能是什么原因是什么? code is as followed:
代码如下:
#include<stdlib.h>
#include<stdio.h>
void display();
struct ll{
int val;
struct ll* address;
};
struct ll *new=NULL,*start=NULL,*present=NULL;
int main(void)
{
int num;
scanf("%d",&num);
//reading ll
new=(struct ll*) malloc(sizeof(struct ll));
new->val=num;
new->address=NULL;
if(start==NULL)
{
start=new;
present=new;
}
else
{
present->address=new;
present=new;
}
//calling display func to display the contents of ll
display();
}
void display()
{
present=start;
// displaying.
while (present!=NULL)
{
printf("%d",present->val);
present=present->address;
}
printf("%d",present->val);
}
I have encoded my comments interspersed into your code.我已经将我的评论编码到你的代码中。 I have commented the last statement in function
display()
, to make it run properly.我已经注释了函数
display()
中的最后一条语句,以使其正常运行。 I have also commented the cast to malloc()
(for the given reasons in the code) I have also made some aesthetic changes to make the code more readable.我还评论了
malloc()
的演员表(出于代码中给定的原因),我还进行了一些美学更改以使代码更具可读性。 You can add spaces to improve readability of the code, as they don't change the compiler produced code, so please, use enough spaces to make your code more readable (I've done this also to show who it is more readable now):您可以添加空格以提高代码的可读性,因为它们不会更改编译器生成的代码,所以请使用足够的空格使您的代码更具可读性(我这样做也是为了显示现在谁更易读) :
#include <stdlib.h>
#include <stdio.h>
void display(void);
struct ll {
int val;
struct ll *address;
};
struct ll *new = NULL,
*start = NULL,
*present = NULL;
int main(void)
{
int num;
scanf("%d", &num);
//reading ll
/* Never cast the returned value of malloc() This allows to
* detect if you have properly #include'd the header file and
* avoids other dificult to find errors. malloc() returns a
* void * pointer, so it will be automatically converted to
* any other pointer type without risk. */
new = /* (struct ll*) */ malloc(sizeof(struct ll));
new->val = num;
new->address = NULL;
if(start == NULL)
{
start = new;
present = new;
}
else
{
/* this is never executed, as start == NULL at program
* start. */
present->address = new;
present = new;
}
//calling display func to display the contents of ll
display();
/* while it is not necessary for main() it is normal for a function
* that is defined to return an int value, to return something, so
* I added the following statement: */
return 0;
}
void display(void)
{
present = start;
// displaying.
while (present != NULL)
{
printf("%d",present->val);
present = present->address;
}
/* as you have moved present in a while loop until the while
* condition is false, at this point you must assume the
* condition is false (so present == NULL) and you are trying to
* dereference a NULL pointer below */
/* printf("%d", present->val); */
}
Now your program will run and show the only value (I recommend you to put a \n
character at the end of the printf()
call, to put the printed data in a line by itself.现在您的程序将运行并显示唯一的值(我建议您在
printf()
调用的末尾放置一个\n
字符,以便将打印的数据单独放在一行中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.