F
farhanb
hello,
I am writing a simple linked list implementation. When I use function
insert1 I must allocate to head in my main to get it to work otherwise
list stays empty but when I use function insert2 there is no need to
allocate to head this is the correct way.
why does this happen? why do we need to have a pointer to a pointer to
head in the insert function for this to work?
thank you
/******************************************************************/
int insert1(int n,NODE *head)
{
NODE *temp,*newnode;
newnode =(NODE*)malloc(sizeof(NODE));
if(newnode == NULL)
{
printf("\ncould not allocate node !!!");
return;
}
newnode->x = n;
newnode->next = NULL;
temp = head;
if(isEmpty(head))
{ head = newnode;
printf("\nallocating to head!");
return; }
else
{ while(temp->next!=NULL && temp!=NULL)
temp = temp->next;
temp->next = newnode;
return newnode->x; }
}
this requires the following in main
NODE *head;
head = (NODE*)malloc(sizeof(NODE));
insert1(4,head);
/**********************************************************************/
int insert2(int n,NODE **head)
{
NODE *temp,*newnode;
newnode =(NODE*)malloc(sizeof(NODE));
if(newnode == NULL)
{
printf("\ncould not allocate node !!!");
return;
}
newnode->x = n;
newnode->next = NULL;
temp = *head;
if(isEmpty(*head))
{ *head = newnode;
printf("\nallocating to head!");
return; }
else
{ while(temp->next!=NULL && temp!=NULL)
temp = temp->next;
temp->next = newnode;
return newnode->x; }
}
this requires the following in main
NODE *head;
head = NULL;
insert(4,&head);
I am writing a simple linked list implementation. When I use function
insert1 I must allocate to head in my main to get it to work otherwise
list stays empty but when I use function insert2 there is no need to
allocate to head this is the correct way.
why does this happen? why do we need to have a pointer to a pointer to
head in the insert function for this to work?
thank you
/******************************************************************/
int insert1(int n,NODE *head)
{
NODE *temp,*newnode;
newnode =(NODE*)malloc(sizeof(NODE));
if(newnode == NULL)
{
printf("\ncould not allocate node !!!");
return;
}
newnode->x = n;
newnode->next = NULL;
temp = head;
if(isEmpty(head))
{ head = newnode;
printf("\nallocating to head!");
return; }
else
{ while(temp->next!=NULL && temp!=NULL)
temp = temp->next;
temp->next = newnode;
return newnode->x; }
}
this requires the following in main
NODE *head;
head = (NODE*)malloc(sizeof(NODE));
insert1(4,head);
/**********************************************************************/
int insert2(int n,NODE **head)
{
NODE *temp,*newnode;
newnode =(NODE*)malloc(sizeof(NODE));
if(newnode == NULL)
{
printf("\ncould not allocate node !!!");
return;
}
newnode->x = n;
newnode->next = NULL;
temp = *head;
if(isEmpty(*head))
{ *head = newnode;
printf("\nallocating to head!");
return; }
else
{ while(temp->next!=NULL && temp!=NULL)
temp = temp->next;
temp->next = newnode;
return newnode->x; }
}
this requires the following in main
NODE *head;
head = NULL;
insert(4,&head);