Reputation: 125
I'm trying to write a function that inserts node at the end of the list.
The problem is that the pointer of the last node of the list doesn't point to NULL and if I show the list I get an error from the system.
struct node{
int value;
struct node *next;
};
struct node *AddToList (struct node *list, int n);
int main()
{
struct node *node;
node = AddToList(node, 30);
node = AddToList(node, 20);
node = AddToList(node, 10);
showlist(node);
return 0;
}
struct node *AddToList (struct node *list, int n){
struct node *new_node;
new_node=malloc(sizeof(struct node));
new_node->value=n;
new_node->next=list;
return new_node;
};
Upvotes: 0
Views: 1745
Reputation: 72
when the user wants to add node at the last first check if it is empty list if so means add the new node as head node.if the list is not empty travese the list and find out the last node by checking tp->next=NULL then store the address of new node in tp->next and make the new node's next field with NULL.the following program show the concept clearly
#include<stdio.h>
#include<malloc.h>
int item,count,pos;
struct node
{
int info;
struct node *next;
}*head,*tp;
void traversal()
{
if(head==NULL)
printf("\n List is empty");
else
{
tp=head;
while(tp->next!=NULL)
{
printf("\n%d",tp->info);
tp=tp->next;
}
printf("\n%d",tp->info);
}
}
void insertlast()
{
struct node *temp;
temp=(struct node*) malloc(sizeof(temp));
printf("\nEnter the element:");
scanf("%d",&item);
temp->info=item;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
tp=head;
while(tp->next!=NULL)
{
tp=tp->next;
}
tp->next=temp;
}
}
int main()
{
printf("\n\t\t**********SINGLY LINKED LIST********");
printf("\n\t------------------------------------------------------");
printf("\n\tInsertion last:");
printf("\n\t---------------------");
insertlast();
traversal();
printf("\n\tInsertion last:");
printf("\n\t---------------------");
insertlast();
traversal();
printf("\n\n\tInsertion last:");
printf("\n\t---------------------");
insertlast();
traversal();
printf("\n\n\tInsertion last:");
printf("\n\t---------------------");
insertlast();
traversal();
return 0;
}
Output
**********SINGLY LINKED LIST********
------------------------------------------------------
Insertion last:
---------------------
Enter the element:12
12
Insertion last:
---------------------
Enter the element:13
12
13
Insertion last:
---------------------
Enter the element:14
12
13
14
Insertion last:
---------------------
Enter the element:15
12
13
14
15
hope that u understand.Thank you
Upvotes: -1
Reputation: 310990
'm trying to write a function that inserts node at the end of the list.
This function
struct node *AddToList (struct node *list, int n){
struct node *new_node;
new_node=malloc(sizeof(struct node));
new_node->value=n;
new_node->next=list;
return new_node;
};
does not insert a node at the end of the list. It inserts a node at the beginning of the list before the head node.
The function that inserts a node at the end of the list can look the following way.
int AddToList ( struct node **list, int n )
{
struct node *new_node = malloc( sizeof( struct node ) );
int success = new_node != NULL;
if ( success )
{
new_node->value = n;
new_node->next = NULL;
while ( *list != NULL ) list = &( *list )->next;
*list = new_node;
}
return success;
}
And the function can be called like
struct node *head = NULL;
AddToList( &head, 10);
AddToList( &head, 20);
AddToList( &head, 30);
if you want that the order of the values of the list would be 10, 20, 30.
The problem is that the pointer of the last node of the list doesn't point to NULL
Because new nodes are inserted before the first node that was not initialized
struct node *node;
and hence has an indeterminate value the last node in the list does not point to NULL.
You have to set the initial pointer to NULL.
struct node *node = NULL;
Take into account that according to the C Standard the function main without parameters shall be declared like
int main( void )
Upvotes: 1
Reputation: 30926
Yes that's because first node that you insert - it's next
is not having the value NULL
.
struct node *node = NULL; //<------
node = AddToList(node, 30);
node = AddToList(node, 20);
node = AddToList(node, 10);
showlist(node);
This will solve the problem. Now as a result of doing this - the very first time you insert the node, it's next
will be assigned the value NULL
. Because for the first call to AddToList
the list
is NULL
.
The way you did it - node
was containing some indeterminate value (garbage value)(node
is a variable with automatic storage duration) and then this is added as link
to the first node. This is of no practical use because now you can't traverse the list thinking that you will find a NULL
value which is when you should stop.
From standard chapter §6.7.9
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
You should check the return value of malloc
. In case it fails - you should handle that case. And free the dynamically allocated memory when you are done working with it.
Also not sure how did you try to show the list, but if it is with the assumption that last node will point to NULL
then started looping over it - then you have earned a Undefined behavior in your code.
Upvotes: 3