Algorithm for concatenation
Let us assume that the two linked lists are referenced by head1 and head2 respectively.
1. If the first linked list is empty then return head2.
2. If the second linked list is empty then return head1.
3. Store the address of the starting node of the first linked
list in a pointer variable, say p.
list in a pointer variable, say p.
4. Move the p to
the last node of the linked list through simple linked list traversal
technique.
the last node of the linked list through simple linked list traversal
technique.
5. Store the address of the first node of the second linked
list in the next field of the node pointed by p. Return head1.
list in the next field of the node pointed by p. Return head1.
Also Read: Menu Driven C Program to Perform Insert, Display and Delete Operations on a Singly Linked List (SLL)
C Function to Concatenate two Linked Lists
node * concatenate (node *head1, node *head2)
{
node
*p;
*p;
if (head1==NULL) //if the first linked
list is empty
list is empty
return
(head2);
(head2);
if (head2==NULL) //if second linked
list is empty
list is empty
return
(head1);
(head1);
p=head1; //place p on the first
node of the first linked list
node of the first linked list
while (p->next!=NULL) //move p to the last node
p=p->next;
p->next=head2; //address
of the first node of the second linked list stored in the last node of the
first linked list
of the first node of the second linked list stored in the last node of the
first linked list
return
(head1);
(head1);
}
Thanks guys..
This code was very usefull and helpfull for me…Thanks again
How would you call this function in main?
Considering this function is a part of a class linkedlist.
Please Provide Newsletter