Algorithm to merge the lists together, Data Structure & Algorithms

Q. Let X = (X1, X2, X3,....Xn) and Y= (Y1, Y2, Y3,....Xm) be the two linked lists respectively. Write down an algorithm to merge the lists together to get the linked list Z such that

Z =  (X1, Y1, X2, Y2,....Xm, Ym,Xm+1....Xn) if m<=n or

Z = (X1, Y1,X2,Y2....Xn,Yn,Yn+1....Ym) if m>n.                                               

Ans.

void near (*head1,*head2,*head3)

{

p1 = head1;

p2 = head2;

p3 = head3;

if (p1 != NULL)

{

p3 = head3 = p1 ;

p1=p1     next ;

}

while (p1!= NULL && p2 != NULL)

{

p3      next = p2 ; p2=p2         next; p3=p3               next;

p3     next = p1; p1 = p1  next ; p3 = p3    next ;

}

for ( ; p1 != NULL; p1 = p1 ?  next)

{

p3      next = p1;

p3 = p3       next ;

}

for ( ; p2 != NULL ; p2 = p2 ?  next)

{

P2       next = p2 ;

p2 = p2   next ;

}

Posted Date: 7/13/2012 2:39:10 AM | Location : United States







Related Discussions:- Algorithm to merge the lists together, Assignment Help, Ask Question on Algorithm to merge the lists together, Get Answer, Expert's Help, Algorithm to merge the lists together Discussions

Write discussion on Algorithm to merge the lists together
Your posts are moderated
Related Questions
1. In computer science, a classic problem is how to dynamically store information so as to let for quick look up. This searching problem arises frequently in dictionaries, symbol t

Your objective is to write a generic doubly linked list class called CS228LinkedList that implements the List interface and uses a type variable T. All methods except for subList a

Searching is the procedure of looking for something: Finding one piece of data that has been stored inside a whole group of data. It is frequently the most time-consuming part of m

Define about the inheritance hierarchy Languages Eiffel and D provide constructs in language for invariants and pre- and post conditions which are compiled into the code and ar


How to create an General Tree and how to search general tree?

Explain in detail the algorithmic implementation of multiple stacks.

This is a k-ary position tree wherein all levels are filled from left to right. There are a number of specialized trees. They are binary trees, AVL-trees, binary search trees, 2

adjacency multilist

Draw a flowchart of a Booth''s multiplication algorithm and explain it.