CEG's Linux User Group

  • Increase font size
  • Default font size
  • Decrease font size
Home Labs Merge lists

Merge lists

E-mail Print
User Rating: / 9
PoorBest 
Article Index
Merge lists
Souce Code
Documentation
Test Cases & Compatibility
All Pages

MERGE LISTS

Introduction :


The objective of this program is to merge two lists into one. Merging two lists will just insert the elements of both the lists into the new one and will not delete the duplicate elements. Consider two lists each of 2 nodes. Let l1 and l2 be the pointers pointing to the first elements of lists 1 and 2 respectively. The lists l1 and l2 are shown below.

mergelist1

The 'temp' pointer is made to point the l1 and is made to traverse the list till NULL is reached. As the list is traversed, the elements of list l1 are inserted in the new merge list,m using the insert() function. The lists l1 and m are shown below.

mergelist2
When the temp pointer reaches NULL , list m contains the elements of list l1. Now, temp is made to point l2, and is made to traverse list l2 till NULL. As the list is traversed all the elements of l2 are inserted in list m using insert().The lists l2 and m are shown below.

mergelist3

Finally when NULL is reached, the list m becomes

mergelist4



Last Updated on Monday, 08 June 2009 16:49  
Please register or login to add your comments to this article.