I have two queues Q1 and Q2 ;
Q1 :A1,A2, A3
Q2: B1, B2, B3
and i want to merge them to newQ:
like A1,B1,A2,B2 and so on...till the both Qs are empty
what is best Algorithm do you think?
I'm kind of confused i was thinking about a priority queue ????

first copy the nodes in each queu until either A or B are empty. After that copy the nodes in the remaining tree until it is empty. If the nodes of each queue are the same structure then you can just copy the address of the nodes from A and B to queue C. If the structures in A and B are different, then you will have to rebuild the nodes of C.

Given q1 and q2 are populated and the resulting queue is q3 I'd do something like:

while(q1.size()+q2.size())
{
  if(q1.size()){ q3.push(q1.front()); q1.pop(); }
  if(q2.size()){ q3.push(q2.front()); q2.pop(); }
}

If you are going to start copying addresses then you need to be aware of the scope of your queues and who is responsible for the cleanup of the elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.