I am learning linked list Here, I am trying to add node at the first so, I made a structure function to add but i dont know why its not working its only giving this output without adding
7 11 66 After insretion 7 11 66
and also please tell me is there any way to insert element on linked list.
// inserting element on first
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node * next;
};
void link_list_tranversal(struct Node *ptr){
while (ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
struct Node * insert_at_first(struct Node *head,int data){
struct Node *ptr=(struct Node *)malloc(sizeof(struct Node));
ptr->next=head;
ptr->data=data;
return ptr;
}
int main(){
struct Node*head;
struct Node*second;
struct Node*third;
head =(struct Node *) malloc(sizeof(struct Node));
second =(struct Node *) malloc(sizeof(struct Node));
third =(struct Node *) malloc(sizeof(struct Node));
// linking first and secend node
head ->data=7;
head->next=second;
// linking secend and third node
second->data=11;
second->next=third;
// terminate the list at the third
third->data=66;
third->next=NULL;
link_list_tranversal(head);
insert_at_first(head,56);
printf("After insretion\n ");
link_list_tranversal(head);
return 0;
}
CodePudding user response:
After insert_at_first(head,56);
, you've created a new node, but head
is now pointing to the second node in the list and you have lost track of the newly created node. Simplest fix is to do: head = insert_at_first(head,56);
. You can also pass &head
and let the function update head
.
CodePudding user response:
You need to update the pointer you are storing in head
after calling insert_at_first
.
// inserting element on first
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node * next;
};
void link_list_tranversal(struct Node *ptr){
while (ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
struct Node * insert_at_first(struct Node *head,int data){
struct Node *ptr=(struct Node *)malloc(sizeof(struct Node));
ptr->next=head;
ptr->data=data;
return ptr;
}
int main(){
struct Node*head;
struct Node*second;
struct Node*third;
head =(struct Node *) malloc(sizeof(struct Node));
second =(struct Node *) malloc(sizeof(struct Node));
third =(struct Node *) malloc(sizeof(struct Node));
// linking first and secend node
head ->data=7;
head->next=second;
// linking secend and third node
second->data=11;
second->next=third;
// terminate the list at the third
third->data=66;
third->next=NULL;
link_list_tranversal(head);
head = insert_at_first(head,56);
printf("After insretion\n ");
link_list_tranversal(head);
return 0;
}
Output:
7
11
66
After insretion
56
7
11
66
CodePudding user response:
The function insert_at_first
returns a new value of the pointer that points to the head node. You need to assign the returned value to the pointer head
to update it
head = insert_at_first(head,56);
Also there is no great sense to allocate nodes in main when you already have the function insert_at_first
You could initially write for example
struct Node *head = NULL;
head = insert_at_first( head, 66 );
head = insert_at_first( head, 11 );
head = insert_at_first( head, 7 );
head = insert_at_first( head, 56 );
Also as the function link_list_tranversal
does not change the outputted list then its parameter should be declared with qualifier const
void link_list_tranversal( const struct Node *ptr );
Pay attention to that this functiondefinition
struct Node * insert_at_first(struct Node *head,int data){
struct Node *ptr=(struct Node *)malloc(sizeof(struct Node));
ptr->next=head;
ptr->data=data;
return ptr;
}
is not good because the function will invoke undefined behavior if memory for a node will not allocated successfully.
It is better to declare and define the function the following way
int insert_at_first( struct Node **head, int data )
{
struct Node *ptr = malloc( *ptr );
int success = ptr != NULL;
if ( success )
{
ptr->next = *head;
ptr->data = data;
*head = ptr;
}
return success;
}
And the function can be called like for example
struct Node *head = NULL;
insert_at_first( &head, 66 );
or
struct Node *head = NULL;
if ( !insert_at_first( &head, 66 ) )
{
puts( "Error. No enough memory" );
}