显示功能在我的链表程序中不起作用? [英] Display function is not working in my linked-list program ?

查看:74
本文介绍了显示功能在我的链表程序中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在链接列表的起始处添加节点我的代码一直只是打印我输入的最后一个值,任何人都可以清除它。



我的尝试:



i am trying to add node at the starting of linked-list my code all the time just printing the last value which i entered what is the problem can any one clear it.

What I have tried:

#include <stdio.h>

struct node
{
    int data;
    struct node *next;

};

typedef struct node node ;
node *head;

void create(int num);
void display();

main()
{
    int num,i,n;

    printf("enter the nno of node to create : ");
    scanf("%d",&n);

    for(i=0;i<n;++i)
    {
        printf("enter data for node %d= ",i+1);
        scanf("%d",&num);
        create(num);
        display();
    }
}

void create(int num)
{
    head=NULL;
    node *temp;
    temp=(node*)malloc(sizeof(node));
    temp->data=num;
    temp->next=head;
    head=temp;
    return;
}
void display()
{
    node *temp1;
    temp1=head;

    while(temp1!=NULL)
    {
        printf("data : %d-> ",temp1->data);
        temp1=temp1->next;
    }
    return;
}

推荐答案

你正在设置头部 NULL 里面 create()并将其(现在 NULL )分配给 next

You are setting the head to NULL inside create() and assign that (now NULL) to next:
void create(int num)
{
    // This sets head to NULL. Remove it.
    head=NULL;
    node *temp;
    temp=(node*)malloc(sizeof(node));
    temp->data=num;
    // This will be NULL because head has been set to NULL above
    temp->next=head;
    head=temp;
    return;
}



因此在声明时删除指示的行并初始化 head


So remove the indicated line and initialise head when declaring it:

node head = NULL;


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



你在解决方案1中得到了答案。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

如果代码没有达到预期的效果,那么您就接近了一个错误。



[更新]

作为解决方案1的补充,请输入此行

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

You have got an answer in solution 1.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

[Update]
In complement to Solution 1, put this line
node head = NULL;

main 的开头。


这篇关于显示功能在我的链表程序中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆