public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2) {

    LinkedQueue out = new LinkedQueue();
    Object item1 = null;
    Object item2 = null;

    int flag = 0;
    int iterate = 0;

    while(!q1.isEmpty() && !q2.isEmpty()){
      try{
        item1 = q1.front();
        item2 = q2.front();

        int compare = ((Comparable) item1).compareTo((Comparable) item2);
        if (compare > 0){
          q2.dequeue();
          out.enqueue(item2);
        }
        else if (compare < 0){
          q1.dequeue();
          out.enqueue(item1);
        }
        else if (compare == 0){
          q1.dequeue();
          q2.dequeue();
          out.enqueue(item1);
          out.enqueue(item2);
        }
      }
      catch(QueueEmptyException e){
        System.out.println("Exception caught in mergeSortedQueues()");
      }
    }

    if (!q1.isEmpty()){
      out.append(q1);
    }
    else if (!q2.isEmpty()){
      out.append(q2);
    }

    return out;
  }