Reputation: 421
Trying on Fedora gcc the following code for a simple Linked List adding new node to the tail of the list. No error in compilation. During execution, it is showing Segmentation Fault, Core Dumped. On MS Windows, it is working.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertion(struct Node *);
void display(struct Node *);
int main(void)
{
struct Node *head;
head=NULL;
head->next=NULL;
int choice, cont;
do
{
printf("1.Insert 2.Display 3.Exit");
scanf("%d",&choice);
if(choice==1)
{
insertion(head);
}
else if(choice==2)
{
display(head);
}
else if(choice==3)
{
exit(0);
}
else
{
printf("Wrong choice");
}
printf("Continue? Press 1 otherwise 0:");
scanf("%d",&cont);
}while(cont==1);
return 0;
}
void insertion(struct Node *start)
{
int data;
struct Node *temp=NULL;
temp->next=NULL;
struct Node *mnew=NULL;
mnew->next=NULL;
mnew=(struct Node *)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&data);
mnew->data=data;
if(start==NULL)
{
start=mnew;
}
else if(start!=NULL && start->next==NULL)
{
start->next=mnew;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=mnew;
}
}
void display(struct Node *start)
{
struct Node *temp=NULL;
temp->next=NULL;
if(start==NULL)
{
printf("\nNothing to display!");
}
else if(start!=NULL && start->next==NULL)
{
printf("%d",start->data);
}
else
{
temp=start;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
}
Your help is appreciated.
Upvotes: 0
Views: 5273
Reputation: 89
As the previous comment, directing a NULL
pointer to another NULL
is not defined (cause a pointer should hold an address).
Now some suggestions:
1) define the struct like so:
typedef struct Node *node_pointer;
this will make it easier to define pointers for that struct.
2)
mnew=malloc(sizeof(*mnew)); //this is easier, and this should be before the ..->next=NULL;
also check if allocation succeed:
if (!mnew)
return; //return something needed
Upvotes: 0
Reputation: 311088
You may not access data using a null pointer. Thus this code snippet (and similar code snippets)
struct Node *head;
head=NULL;
head->next=NULL;
^^^^^^^^^^^^^^^
is invalid.
As for the function insertion
then you have to pass the head by reference. Otherwise the function will deal with a copy of the head and any changes of the copy of the head in the function will not influence on the original head.
Also if the memory allocation will failure it is desirable that the function signals about this. So instead of the return type void
it is better to use the return type int
.
So the function declaration can look like
int insertion( struct Node ** );
^^^ ^^^^^^^^^^^^^^
The function can be defined like
int insertion( struct Node **start )
{
int data;
printf( "Enter data: " );
scanf( "%d", &data );
struct Node *temp = ( struct Node * )malloc( sizeof( struct Node ) );
int success = temp != NULL;
if ( success )
{
temp->data = data;
temp->next = NULL;
while ( *start ) start = &( *start )->next;
*start = temp;
}
return success;
}
The function can be called the following way
insertion( &head );
The function display
can look like
void display( struct Node *start )
{
if ( start == NULL )
{
printf( "\nNothing to display!\n" );
}
else
{
for ( ; start; start = start->next )
{
printf( "%d ", start->data );
}
putchar( '\n' );
}
}
Upvotes: 1
Reputation: 8142
head=NULL;
head->next=NULL;
This piece of code could never work as you cannot access or assign values to attributes of head
if it's pointing to NULL (aka nowhere).
Upvotes: 5
Reputation: 409404
Take a closer look at e.g. these two lines from the insertion
function:
struct Node *temp=NULL;
temp->next=NULL;
The first defines a pointer to struct Node
and makes it be a null pointer. The very next line you dereference this null pointer, which is invalid and leads to undefined behavior.
You have the same problem in multiple places, both exactly like this, and also dereferencing null pointers in general.
Upvotes: 2