-4

When entering value in head node it works but when creating mid nodes it gives segmentation fault. What changes should i make?

Tried making various changes also tried to remove segmentation fault by looking upon NULL instances but failed. Beg your pardon if the question seems silly but i tried really hard.

#include <iostream>

using namespace std;
class node
{
    public:
    int data;
    node *next;
};
node *head,*pre,*curr,*tail;
void createhead(int roll)
{
    head->data=roll;                                   //creating head
    head->next=NULL;
    pre=head;
}
 void createlist(int roll)
{
    curr->data=roll;                                    //creating new node
    curr->next=NULL;                                    //in this two            lines.
    pre->next=curr;
    pre=curr;
}
void disp()
{
    curr=head;
    while(curr!=NULL)
    {
        cout<<"Value---- \t"<<curr->data<<"\n";
        curr=curr->next;
    }
}
int main()
{
    head =new node();
    pre=new node();
    tail=new node();
    cout<<"Enter 999 to stop\n";
    int roll;
    cout<<"enter roll number\n";
    cin>>roll;
    if(roll!=999)
    createhead(roll);
    for(;;)
    {
        cout<<"enter roll number\n";
        cin>>roll;
        if(roll==999)break;
        createlist(roll);
    }
    disp();
}

Expected to create a complete linked list.

  • 2
    You seem to use `cur` before it's been given a value. – Carcigenicate May 18 '19 at 16:45
  • 1
    Your nodes seem to have their `next` pointers uninitialized, so you _should not_ expect any reasonable results from using them. – CiaPan May 18 '19 at 16:50
  • 1
    *Tried making various changes* -- This is not how to debug a program. You're supposed to pinpoint what is wrong first, and *then* adjust the code once you identified the issue. Just changing code by random, hoping that things just "work" isn't the way to approach debugging an issue. Also, it should be obvious that `curr` is a `nullptr` -- you even have a comment on the line that uses it. So you didn't check that line to make sure `curr` is not `nullptr`? – PaulMcKenzie May 18 '19 at 16:53
  • Two things make linked lists easier to manage: 1. Draw pictures to help you visualize what the list must look like and trying to draw the same picture by following your coded instructions. When you can't draw the same picture, you've found a bug. 2. The debugging utility that should have come with your development environment. Every development environment worth using since the 1980s has had a debugger. If yours doesn't, ditch that sack of and get one that does. – user4581301 May 18 '19 at 16:53
  • https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – Jesper Juhl May 18 '19 at 17:00

1 Answers1

1

The idea for linked lists is when you are adding a node to the ends of the list, you do two things:

  • create a new item
  • insert the item into the list
  • reassign the inserted node to be the head

enter image description here

Here's an example with your code:

void createlist(int roll)
{
    // create a new node for the roll
    curr = new node();
    curr->data = roll;
    // point the next node to the head of the list (adds it to the front)
    curr->next = head;
    // now curr is the head
    head = curr;
}

A similar method could be used to append the item to the tail.

Inserting an item in the middle of the list would require indexing into the list and setting the next pointer of the indexed node to point to the new node and setting the next pointer of the new node to point to the indexed nodes next pointer.

enter image description here

jackw11111
  • 1,457
  • 1
  • 17
  • 34