-1

I am trying to create a singly linked list by inserting nodes at end, and despite having no errors I am unable to print my linked list. Please help me debug my code.

I tried online compiler on codechef and it shows SIGSEGV Runtime error. What is this supposed to mean?

struct node
{
int data;
struct node *next;
};

void insert(struct node *root,int data)
{
struct node *temp=new(struct node);

if(root==NULL)
{
    temp->data=data;
    temp->next=NULL;
}

root->next=temp;
temp->data=data;
temp->next=NULL;

}

void print(struct node *root)
{
struct node *temp;
temp=root;
while(temp!=NULL)
{
    cout<<temp->data;
            temp=temp->next;

}

}

int main()
{
struct node *root=NULL;
insert(root,1);
    insert(root,2);
insert(root,3);
insert(root,4);
print(root);
return 0;
}
  • 4
    _"Please help me debug my code."_ Nope! Stack Overflow isn't a free online debugging service. There are ways you can help yourself even online, e.g.: https://www.onlinegdb.com/. – πάντα ῥεῖ Aug 17 '19 at 14:52
  • It means you're code invokes undefined behavior. Your `insert` is broken in multiple places. – WhozCraig Aug 17 '19 at 14:52
  • 1
    "it shows SIGSEGV Runtime error. What is this supposed to mean?" - A segmentation fault (SIGSEGV) means that your program tried to read or write memory outside its allocated address space. This is a fatal error and the kernel (rightly) killed the program for the violation. – Jesper Juhl Aug 17 '19 at 14:55
  • 1
    Please don't use `NULL` in new code. Use `nullptr`. – Jesper Juhl Aug 17 '19 at 14:56
  • You may want to spend a bit of time investigating [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html)... – Jesper Juhl Aug 17 '19 at 14:58
  • "I am trying to create a singly linked list" - instead of just using [std::forward_list](https://en.cppreference.com/w/cpp/container/forward_list) - *Why*? – Jesper Juhl Aug 17 '19 at 15:01
  • _"despite having no errors"_ -- Are you sure about this? Being unable to print is an error. A SIGSEGV runtime **error** is an error. Lots of errors. -- Perhaps you meant no compile-time errors? (Not that the lack of compile time errors should ever be taken as evidence that a program will work as intended. See also [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/).) – JaMiT Aug 17 '19 at 15:30

1 Answers1

2

Please help me debug my code.

OK lets try a dry run.

Imagine your list is empty and you are inserting your first item. So root equals NULL and we call insert.

1) first thing

struct node *temp=new(struct node);

You allocate a new node, and set temp equal to it, so far so good.

2) next thing

if(root==NULL)

this is true as explained in the preamble, so we enter the if statement

3) next thing

temp->data=data;
temp->next=NULL;

these statements in the if body get executed and initialise the newly allocated object. It's not clear why you only want to do this when root == NULL, I would think you would want to initialise the newly allocated node always. But anyway, so far no errors.

4) next thing

root->next=temp;

Now here's the error. Ask yourself, what is the value of root at this point? When we started it was NULL, has anything changed it since? The answer of course is no, so you are dereferencing a NULL pointer. That explains the error.

You need to be able to look at the code you've written and see it for what it really does. The ability to dry run your code like I did above is a very valuable skill to have.

Unfortunately your code really is not very close to being correct. So I think the best thing would be to look at some working code and see how it operates and then start again.

john
  • 85,011
  • 4
  • 57
  • 81
  • I didn't see this struct node *temp=new(struct node); it's everywhere – Oblivion Aug 17 '19 at 15:19
  • 1
    @Oblivion I was going to comment on the dubious syntax, but I thought it better to concentrate in the main point. For the OP, that line is better written as `node* temp = new node;` – john Aug 17 '19 at 15:20
  • @john i wanted to create an initial node by using **if(root==NULL)** ; i tried making a head and tail node instead and it works. if(head == NULL){head = temp; tail =temp; } else {tail->next = temp;tail = tail->next; } Thanks a lot :) – ANOUSHKA TRIVEDI Aug 17 '19 at 15:59
  • @john the OP has used C style. I found this: https://stackoverflow.com/q/39838629/10933809 – Oblivion Aug 21 '19 at 13:54