发布此消息之前,我经历了堆栈溢出的一些以前的帖子。 每次我尝试在ideone.com上运行这个(它使用SPOJ引擎)时,我得到一个SIGSEGV运行时错误,我很难过。 任何人都可以请帮我吗?香港专业教育学院的评论,我觉得我有可能出了错线..在链接列表中插入时出现SIGSEGV错误
//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;
}
你的代码甚至没有接近正确的。 'display()'函数“忘记”链表的原始开始位置,你的'insert()'的代码在JUST FINISHED确认它为NULL之后解引用一个指针。请Google提供一个简单链接列表实现的实例,并从此开始。 – Celada 2012-01-13 04:51:59
或者更好的是,拿一本书并开始阅读 – Aamir 2012-01-13 04:52:45
谢谢您的意见@ Celada.Shall对他们的工作 – wave5459 2012-01-13 05:18:00