Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:04:31+00:00 2026-05-28T04:04:31+00:00

Ive gone through some of the previous posts on Stack Overflow before posting this.

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:04:32+00:00Added an answer on May 28, 2026 at 4:04 am

    You are not allocating memory to the Node. You need to do this in your insert function:

    node = new Node();
    

    And only after that, you should start filling out data and link in your node. Do this in both if and else cases.

    The corrected code will be something like the following (I haven’t run it through a compiler, so check for compiler errors):

    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;
    }
    } 
    

    You also need to delete all the allocated nodes later on after you have finished working with the list, otherwise, it will be a memory leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've gone through most of the example code and I still need some help.
I'm developing a Silverlight application for the first time. I've gone through some tutorials,
I'm writing an 8086 assembler for a college project . I've gone through some
For some reason, whenever I now try and run my SL4 application out-of-browser, when
Ok so I've gone through pretty much all the SO articles on this subject
I've gone through quite a few articles on the repository pattern and had some
I've gone through just about every property I can think of, but haven't found
I've gone through a few Java questions on SO. And I must say the
I've gone through the academic Scheme stuff (read SICP, The Little Schemer, The Seasoned
I've gone through the Facebooker msg archive, google, etc and found a couple messages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.