import java.util.LinkedList;
import java.util.Queue;

public class QueueMerger {
    
    public Queue<Integer> mergeQueues(Queue<Integer> a, Queue<Integer> b) {
        Queue<Integer> mergedQueue = new LinkedList<Integer>();
        // gets peeks of each queue if both are not empty
        while (a.peek() != null && b.peek() != null) { //!a.isEmpty() && !b.isEmpty()) { needed to define is empty in t but having problems 


            if (a.peek() < b.peek()) {
                mergedQueue.add(a.peek());
                a.remove();
            } 
            else {
                mergedQueue.add(b.peek());
                b.remove();
            }
        }
        // if b is empty but a is not add the first element to the new merged queue and removes the non empty 
        while (a.peek() != null) {
           mergedQueue.add(a.peek());
           a.remove();
        }
        // if a is empty but b is not add the first element to the new merged queue and removes the non empty 
        while (b.peek() != null) {
            mergedQueue.add(b.peek());
            b.remove();
        }

        return mergedQueue;
    }

    public void printQueue(Queue<Integer> queue) {
        System.out.print("Queue: ");
        for (Integer addedNums : queue)
            System.out.print(addedNums + " ");
        System.out.println();
    }
    
    public static void main(String[] args) {
        // add numbers for two queues to merge 
        Queue<Integer> queue1 = new LinkedList<Integer>();
        Queue<Integer> queue2 = new LinkedList<Integer>();

        // add objects to queue and print both
        queue1.add(1); //check to make sure no mistakes in the middle mix up and instead 
        queue1.add(2);
        queue1.add(3);
        queue1.add(4);
        queue1.add(5);
        queue1.add(6);
        queue2.add(7);
        queue2.add(8);
        queue2.add(9);
        queue2.add(10);
        queue2.add(11);
        queue2.add(12);
        

        // merge queues
        QueueMerger mergingQueues= new QueueMerger();
        //print the first queues to display
        mergingQueues.printQueue(queue1); // or use qm to be more precise with previous code but i merging queues makes more sense to me 
        mergingQueues.printQueue(queue2);

        Queue<Integer> merged = mergingQueues.mergeQueues(queue1, queue2);

        // print merged queue
        mergingQueues.printQueue(merged);
    }
}

QueueMerger.main(null);
Queue: 1 2 3 4 5 6 
Queue: 7 8 9 10 11 12 
Queue: 1 2 3 4 5 6 7 8 9 10 11 12