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
Optimal solution to the problem given below. Obtain the initial solution by VAM Ware houses Stores Availibility I II III IV A 5 1 3 3 34 B 3 3 5 4 15 C 6 4 4 3 12 D 4 –1 4 2 19 Re

State  in brief about assertion Assertion: A statement which should be true at a designated point in a program.

Threaded Binary Tree:- By changing the NULL lines in a binary tree to special links known as threads, it is possible to perform traversal, insertion and deletion without using

Explain binary search with an example

Write a C++ program with header and source les to store street addresses using the Doubly Linked List ADT. Modify the Node class from Lab Assignment 3 so that it becomes a node in

1. Apply the variant Breadth-First Search algorithm as shown in Figure 2 to the attached graph. This variant is used for computing the shortest distance to each vertex from the sta

A list item stores pointers and an element to predecessor and successor. We call a pointer to a list item a handle . This looks simple enough, but pointers are so powerful tha

How to construct a data flow diagram for a college assignment and marking systemA

Regis lives in Brazil and frequently travels to USA, Japan and Europe. He wants to be able to convert Brazilian Reais into US dollars, European euros and Japanese yen. Conversion f

write an algorithm to delete an element x from binary search with time complex