Write a programme that allows to merge two sorted queues ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Write a programme that allows to merge two sorted queues !

31st Dec 2017, 10:14 AM
Ikrame Ounadi
Ikrame Ounadi - avatar
6 Answers
+ 4
Apply Merge sort. 1) Make pointers for both of the queues. 2) Make another queue, add the node having least value from the other two queues(the pointer's node having the smallest value) to it and move that pointer to the next node. 3) Again compare the node's values and add the node having the smallest value to the queue and move that pointer to the next node. 4) Keep doing this until rear of both the queues are reached.
31st Dec 2017, 6:18 PM
Saumya
Saumya - avatar
+ 3
Are these queues the STL queues or custom type queues? If its the second one, are they implemented using plain arrays or linked lists? For first, you do : // q1 and q2 are the initial queues. queue<T> q3; // T is the type of elements of q1||q2. // The main algorithm remains the same, // just some change of names depending // on the implementation // For ascending order sorting only. // Change > to < for descending. // The current implementation empties // the original queues, so make a copy if // you need the queues somewhere later. while(!q1.empty() && !q2.empty()) { if(q1.front()>q2.front()) { q3.push_back(q1.front()); q1.pop_front(); } else { q3.push_back(q2.front()); q2.pop_front(); } } // The following loops help add the rest // of the elements that the above loop // did not add. while(!q1.empty()) { q3.push_back(q1.front()); q1.pop_front(); } while(!q2.empty()) { q3.push_back(q2.front()); q2.pop_front(); }
31st Dec 2017, 4:55 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
But have you made a class for the Queue or you just use the Node pointers start and rear (or similar names) in normal functions? If you just use pointers: /* Assumes the node structure as : struct node { T info; node* next; }; And the queue functions to be enqueue to add, and dequeue to remove. Assumed prototype: void enqueue(node* start, node* end, T elm); void dequeue(node* start, node* end); */ node *s3,*r3; s3 = r3 = nullptr; // For ascending order sorting only. // Change > to < for descending. while(s1!=nullptr && s2!=nullptr) { if(s1->info>s2->info) { enqueue(s3,r3,s1->info); dequeue(s1,r1); } else { enqueue(s3,r3,s2->info); dequeue(s2,r2); } } // The following loops help add the rest // of the elements that the above loop // did not add. while(s1!=nullptr) { enqueue(s3,r3,s1->info); dequeue(s1,r1); } while(s2!=nullptr) { enqueue(s3,r3,s2->info); dequeue(s2,r2); }
31st Dec 2017, 6:23 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
no it is with linked lists
31st Dec 2017, 5:56 PM
Ikrame Ounadi
Ikrame Ounadi - avatar
0
Have you implemented a class for the same? Or you use raw start and rear pointers?
31st Dec 2017, 6:02 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
i use pointers
31st Dec 2017, 6:14 PM
Ikrame Ounadi
Ikrame Ounadi - avatar