I have a simple program to insert elements at the end of the linked list using a function call.
My Code:
struct Date{
int data;
struct Date *next;
};
void print(struct Date *date_head);
void add(int date, struct Date *date_head);
int main(){
struct Date *date_head=NULL;
add(12,date_head);
add(15,date_head);
}
void print(struct Date *date_head){
struct Date *ptr=date_head;
printf("Dates: ");
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
void add(int date, struct Date *date_head){
//newDate
struct Date *newDate;
newDate = (struct Date *)malloc(sizeof(struct Date));
newDate->data=date;
newDate->next=NULL;
//inserting newDate at end
struct Date *date_ptr;
date_ptr=date_head;
if (date_ptr==NULL) {date_head=newDate;}
else{
while(date_ptr->next!=NULL){
date_ptr=date_ptr->next;
}
date_ptr->next=newDate;
}
print(date_head);
}
What I want:
Dates: 12
Dates: 12 15
What I get:
Dates: 12
Dates: 15
The struct becomes NULL outside the add function. Even when I am using pointers. Why?
I dont know if I am making a beginner mistake.
CodePudding user response:
It is better at least to swap the parameters of the function like
void add( struct Date *date_head, int date );
The function accepts the pointer to the head node declared in main by value. That is the function deals with a copy of the value of the pointer. Changing the copy within the function does reflect on the original pointer.
Also the memory allocation within the function can fail. You should report such a situation to the caller of the function.
So the function should return an integer saying whether the allocation of the new node was successful and it should accept the pointer to the head node by reference that is through a pointer to it.
Taking all this into account the function declaration and definition will look the following way.
int add( struct Date **date_head, int date )
{
struct Date *newDate = malloc( sizeof( struct Date ) );
int success = newDate != NULL;
if ( success )
{
newDate->data = date;
newDate->next = NULL;
while ( *date_head != NULL ) date_head = &( *date_head )->next;
*date_head = newDate;
}
return success;
}
Within main you should write
struct Date *date_head=NULL;
add( &date_head, 12 );
add( &date_head, 15 );
Also the parameter of the function should have the qualifier const because the list is not being changed within the function.
void print( const struct Date *date_head );
And if you want to append new node to the tail of the list then it is better to declare a twp-sided singly-linked list.
CodePudding user response:
you are passing the value of date_head pointer to the add function, so the add function will have another copy of that pointer, and even if you change the value of this pointer in the add function and set it to some address number, it will not affect the date_head that you created in the main, so it will be always null, what you can do is pass it by the address instead:
int main(){
struct Date *date_head=NULL;
add(12,&date_head);
add(15,&date_head);
}
and edit the add function to be like this :
void add(int date, struct Date **date_head){
//newDate
struct Date *newDate;
newDate = (struct Date *)malloc(sizeof(struct Date));
newDate->data=date;
newDate->next=NULL;
//inserting newDate at end
struct Date *date_ptr;
date_ptr=*date_head;
if (date_ptr==NULL) {*date_head= newDate;}
else{
while(date_ptr->next!=NULL){
date_ptr=date_ptr->next;
}
date_ptr->next=newDate;
}
print(*date_head);
}
