English 中文(简体)
SIGSEGV错误,同时在相关名单内插入
原标题:SIGSEGV error while inserting in Linked List

Ive gone through some of the previous posts on Stack Overflow before posting this. Everytime I try to run this on ideone.com (which uses the SPOJ engine) I get a SIGSEGV runtime error and I am stumped. Can anyone please help me out? Ive commented the line where I think I might have gone wrong..

//MY CODE TO INSERT INTO A LINKED LIST
#include<iostream>
#include<malloc.h>

using namespace std;

struct Node
{
int data;
struct Node *link;
};
struct Node *node =NULL; //suspected error..not sure

void insert(int item)
{
if(node==NULL)
{
       node->data=item;
       node->link=NULL;
}
else 
{
    struct Node *temp;
    temp=node->link;
    node->data=item;
    node->link=temp;
}
} 

void display()
{
if(node==NULL)
    cout<<"Linked list is empty!";
while(node!=NULL)
{
    cout<<node->data<<" ";
    node=node->link;
}
}         
int main()
{
int n;
    cin>>n;
    display();
    insert(n);

      display();
return 0;
}
最佳回答

你们不会把记忆传给诺德。 在你的职责中插入:

node = new Node();

之后,你应开始填写<代码>和/code>。 在<代码>if/code>和上填写。

经更正的法典将像以下(我没有通过汇编者加以操作,从而核对汇编者错误):

void insert(int item)
{
if(node==NULL)
{
       node = new Node();
       node->data=item;
       node->link=NULL;
}
else 
{
    struct Node *temp = new Node();
    temp=node->link;
    node->data=item;
    node->link=temp;
}
} 

在你完成清单工作之后,你还需要删除所有已分配的节点,否则,这将是一个记忆泄露。

问题回答

您从来没有为新的<代码>分配记忆。 您正在创建。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?