我的链表列表的C ++代码有什么问题 [英] What is wrong in my C++ code for linked list

查看:101
本文介绍了我的链表列表的C ++代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定我的c ++编码出了什么问题



没有编译器错误但是一旦我运行我的程序它说我的程序已经停止工作。







i我刚学习c ++而且我的第14天用c ++学习



我尝试了什么:



I cant identify what is going wrong in my c++ coding

there is no compiler error but as soon as I run my program it says MY PROGRAM HAS STOPPED WORKING.



i am just learning c++ and its my 14th day with c++

What I have tried:

#include <iostream>

using namespace std;

struct node
{
    int number;
    node* next;
};
node*head=NULL;
bool emptylist(node* head)
{
    if(head==NULL)
    {
        return true;
    }
    else{
        return false;
    }
}

void insertfirstelement(node*temp,int numb)
{
    node*temp1= new node;
    temp->number=numb;
    temp->next=NULL;

    temp=head;
}
void insert_(node*temp,int numb)
{
    node*temp1=new node;
    temp1->number=numb;
    temp1->next=NULL;
    head->next=temp1;

}
int main()
{
if (emptylist)
{
    node*temp1;
    insertfirstelement(temp1,5);
    insert_(temp1,3);
    insert_(temp1,4);

}

cout << head->next->number;
}

推荐答案

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



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

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

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



你的代码有很多问题,这行是内存泄漏。

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[^]

Your code have numerous problems, this line is a memory leak.
node*temp1= new node;



使用调试器逐行运行代码,你会学到很多东西。



建议:拿一张纸说明如何列表在开始时以及在添加新节点时如何演变,然后检查代码是否与更改匹配。


Run your code line by line with the debugger, you will learn a lot.

Advice: Take a sheet of paper and note how is the list on start and how it evolve as you add new nodes, then check if your code match the changes.


显然,在主体中声明未初始化的 temp1 node然后在 insertfirstelement 函数中使用它。



在C ++中,它是up为了确保变量在使用之前被初始化。编译器可能会警告明显的情况,但不会进行全局分析...



实际上,它看起来像整个 insertfirstelement 函数编码不正确。由于此时列表为空,您必须创建头部,而不需要将任何节点传递给该函数。



我可以给代码但是然后你就不会学到任何东西。



另外,正如其他人告诉你的那样,使用调试器是个好主意。使用这些工具,您可以在比编写该问题所花费的时间更短的时间内找到问题的根源......
Obviously, in your main you declare an uninitialized temp1 node and then use it in insertfirstelement function.

In C++, it is up to you to ensure that variable are initialized before they are used. Compiler might warns about obvious cases but won't do global analysis...

In fact, it look like the whole insertfirstelement function is improperly coded. Since the list is empty at that point, you have to create the head and you don't need to pass any node to that function.

I could give code but then you won't learn anything.

Also, as others have told you, it would be a good idea to use a debugger. Using such tools, you can find the root of such problem in less time than it took to write that question...


这篇关于我的链表列表的C ++代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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