-4

I have been trying to fix this code since morning but could get it done. So, finally i need some help in figuring out the error. The code compiles with no error but when i run it from terminal i get an error saying "segmetation error: 11"

#include <stdio.h> 
#include <stdlib.h> 


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


struct Node *head; 


void Insert(int data); 
void Print();  


int main()
{
    head = NULL; //list is empty

    Insert(3); 
    Insert(5); 
    Insert(2); 
    Insert(8); 

    Print(); 

    return 0;  
} 


void Insert(int data) 
{
    struct Node *temp = malloc(sizeof(struct Node)); 

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

    struct Node *temp1 = head;

    while(temp1 != NULL) 
    { 
        temp1= temp1->next; 
    } 

    temp1->next = temp; 
}


void Print()
{ 
    struct Node *temp =head; 

    while(temp != NULL)
    { 
        temp = temp->next; 
        printf("%d", temp->data); 
    } 
}
2
  • What line does it crash on? Oct 4, 2014 at 14:21
  • temp1->next = temp; : temp1 is NULL
    – BLUEPIXY
    Oct 4, 2014 at 14:22

4 Answers 4

2
  1. You never set head to anything other than NULL.

  2. (Even if you fix the above) temp1 is guaranteed to be NULL by the time you get to temp1->next = temp;.

P.S. I don't think it's such a great practice to call you variables temp, temp1 etc. From those names it is impossible to tell what their supposed function is.

0

Usually single linked lists have an inserting operation that inserts data at the beginning of the list. Nevertheless your function insert can look the following way

void Insert( int data ) 
{
    struct Node *temp = malloc(sizeof( struct Node ) ); 
    temp->data = data; 
    temp->next = NULL; 

    if ( head == NULL )
    {
        head = temp;
    }
    else
    {
        struct Node *current = head;
        while ( current->next != NULL ) current = current->next;
        current->next = temp;
    }
}

It would be better if you check in the function whether the node was allocated.

Function Print also is wrong. It can look like

void Print( void )
{ 
    for ( struct Node *current = head; current != NULL; current = current->next )
    { 
        printf( "%d ", current->data ); 
    } 
}
0

You are writing

while(temp1 != NULL)

Make it like this

while(temp1->next != NULL)

Here, temp1 points to null at the end of loop, and you are trying to add node after null i.e.

null->next =temp;

which must be throwing error.

0
struct Node *curr;

void Insert(int data){
    struct Node *temp = malloc(sizeof(struct Node));
    temp->data = data;
    temp->next =NULL;

    if(head == NULL)
        curr = head = temp;
    else
        curr = curr->next = temp;
}

void Print(){
    struct Node *temp =head; 
    while(temp != NULL){ 
        printf("%d ", temp->data); 
        temp=temp->next; 
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.