简体   繁体   English

我正在尝试读取和打印链表中的一个值,但我的程序没有给出任何输出

[英]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.

相关问题 我正在尝试在链表中插入值,但在 output 中它只显示第一次插入 - I am trying to insert value in linked list but in output it is showing only first insertion 为什么我的链表程序会无限地打印出我输入的那个“第一个数字”的循环? - Why does my linked-list program print out a loop of that "first number" I entered, infinitely? 我无法使用链表打印我的多项式表达式 - I am not able to print my polynomial expression using linked list 我正在尝试对单个链接列表进行排序 - I am trying to sort a single linked list 为什么我的 ft_print_comb function 没有给出任何 output? - Why my ft_print_comb function does not give any output? 为什么在此程序中printf(i)给出0作为输出? - Why does printf(i) give 0 as output in this program? 尝试打印链接列表节点时,程序正在关闭 - When trying to print a linked list nodes the program is turning off 为什么我的程序给我多余的输出? C程序 - Why does my program give me the excess output? C program 我正在尝试使用 C 中的递归来反转链表,我对我的递归 function 有一些疑问 - I am trying to reverse a linked list using recursion in C, I have some doubts on my recursive function 我会覆盖链接列表吗? - Am I overwriting my linked list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM