-->

Saturday, March 1, 2014

Merging the ordered Singly Linked Lists in 'C'

 Merging the ordered Singly Linked Lists in 'C'

The merging is over. The merged list can be traversed with the help of the external pointer ROOT. ROOT points to the node with information 1. Node with information 1 points to the node with information 10. Next node is the node with information 20. Similarly the nodes with information 23 and 25 are the next node. The node with information 25 is the last node of the merged list.

Algorithm to merge two ordered linked lists:

MERGEOLLS(ROOT1,ROOT2)
IfROOT-->INFO<ROOT2-->INFO Then:
  ROOT<--ROOT1;  ROOT1<--ROOT1-->LINK
Else:
  ROOT<--ROOT2;  ROOT2<--ROOT2-->LINK
[End of If]
PTR<--ROOT
Repeat While ROOT1< >NULL AND ROOT2< >NULL
  If ROOT1-->INFO<ROOT2-->INFO Then:
      PTR-->LINK<--ROOT1
      PTR<--PTR-->LINK
 Else:
      PTR-->LINK<--ROOT2
      PTR<--PTR-->LINK
      ROOT2<--ROOT2-->LINK
  [End of If]
[End of While]
If ROOT1 = NULL Then:
  PTR<--ROOT2
[End of If]
If ROOT2 = NULL Then:
   PTR<--ROOT1
[End of If]
Exit.

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved