Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* * mergeQueues * * precondition: input queues q1, q2 may be empty * postcondit

ID: 3746166 • Letter: #

Question

/*

* mergeQueues

*

* precondition: input queues q1, q2 may be empty

* postconditions:

* return a new queue containing merged contents of q1,q2 using

* an every-other-one policy - starting with q1

* if queues are not the same length, the 'tail' of the longer one ends

* up getting appended to the result - see examples below

*

* original queues are empty

*

* you may use: basic java arrays, Stack<>, Queue<> from algs13

* you may NOT use any other Java classes, algorithms without permission

*

* your solution must be all client code: you may not alter the original Stack, Queue classes

*

* In the examples below, the left-most values are at the front of the queues

* Examples:  

* q1: abcd , q2: wxyz mergeQueues(q1,q2) --> awbxcydz

* q1: abc , q2: xy mergeQueues(q1,q2) --> axbyc

* q1: ab , q2: xyz mergeQueues(q1,q2) --> axbyz

* q1: abc , q2: mergeQueues(q1,q2) --> abc

* q1: , q2: mergeQueues(q1,q2) -->

* merge

*/

public static Queue<String> mergeQueues(Queue<String> q1,Queue<String> q2) {

return new Queue<String>(); // To Do 2 fix this

}

/*

* equalQueues

*

* two queues are equal if they are the same size and all corresponding items are the same

* precondition: input queues q1, q2 may be empty

* postconditions:

* return true if the two queues are identical and false otherwise

* original queues are unchanged

*

* you may use: basic java arrays, Stack<>, Queue<> from algs13

* you may not use any other Java classes, algorithms without permission

*

* your solution must be all client code: you may not alter the original Stack, Queue classes

*

* you MAY NOT use the toString method of the Stack or Queue classes

*

* In the examples below, the left-most values are at the front of the queues

* Examples:  

* q1: abcd , q2: abcd equalQueues(q1,q2) --> true

* q1: abc , q2: xy equalQueues(q1,q2) --> false

* q1: a , q2: xyz equalQueues(q1,q2) --> false

* q1: a , q2: equalQueues(q1,q2) --> false

* q1: a , q2: a equalQueues(q1,q2) --> true

*

*/

public static boolean equalQueues( Queue<String> q1, Queue<String> q2) {

return false; // To Do 3 fix this

}

Explanation / Answer

import java.util.*;

  

class MergedQueue

{

    public static Queue<String> mergeQueues(Queue<String> q1,Queue<String> q2)  

    {

        // Creating an empty merged queue  

        Queue<String> merged_q = new LinkedList();

        // If q1 is empty, then adding the q2 elements only

        if(q1.size() == 0)

        {

            int size = q2.size();

            for(int i=0; i < size; i++)

            {

                merged_q.add(q2.remove());

            }

        }

        // If q2 is empty, then adding the q1 elements only

        else if(q2.size() == 0)

        {

            int size = q1.size();

            for(int i=0; i < size; i++)

            {

                merged_q.add(q1.remove());

            }

        }

        // If both queues contains elements

        else

        {

            // If q1 has less number of elements than q2

            if(q1.size() <= q2.size())

            {

                int size = q1.size();

                // Adding elements from both queues one by one

                for(int i=0; i < size; i++)

                {

                    merged_q.add(q1.remove());

                    merged_q.add(q2.remove());

                }

                // Adding the extra elemets remained in q2

                for(int i=0; i < q2.size(); i++)

                {

                    merged_q.add(q2.remove());

                }

            }

            // If q2 has less number of elements than q1

            else

            {

                int size = q2.size();

                // Adding elements from both queues one by one

                for(int i=0; i < size; i++)

                {

                    merged_q.add(q1.remove());

                    merged_q.add(q2.remove());

                }

                // Adding the extra elemets remained in q2

                for(int i=0; i < q1.size(); i++)

                {

                    merged_q.add(q1.remove());

                }

            }

        }

        return merged_q;  

    }

     

    public static boolean equalQueues( Queue<String> q1, Queue<String> q2)

    {

        // If size is not equal return false directly

        if(q1.size() == q2.size())

        {

            String strArray1[] = q1.toArray(new String[q1.size()]);

            String strArray2[] = q2.toArray(new String[q2.size()]);

            // if size is equal, compare each element in two queues one by one    

            for(int i=0; i < q1.size(); i++)

            {

                if(strArray1[i] != strArray2[i])

                {

                    return false;

                }

            }

            return true;

        }

        return false;  

    }    

}

  

// NOTE: Extra class to test the written code, remove this if you want

public class Main

{

    public static void main(String[] args) {

         

        Queue<String> q1 = new LinkedList();

        q1.add("a");

        q1.add("b");

        q1.add("c");

        System.out.println(q1);

         

        Queue<String> q2 = new LinkedList();

        q2.add("x");

        q2.add("y");

        q2.add("z");

        System.out.println(q2);

         

        System.out.println(MergedQueue.equalQueues(q1, q2));

         

        System.out.println(MergedQueue.mergeQueues(q1, q2));

    }

}

OUTPUT

[a, b, c]

[x, y, z]

false

[a, x, b, y, c, z]