Syntax Error on Bottom-Up-Merge-Sort code. How do I fix this?

时间:2019-04-16 22:14:29

标签: java queue mergesort

There's a syntax error in this code, and I'm not sure how to get it to run. This is code from a Chegg Textbook Solution, "Algorithms (4th Edition)" Chapter 2.2 Problem 14E and 15E.

The following lines (lines 10 and 11 of the first class) are causing the error:

Queue> ques = new

LinkedList>();

All quick suggestions have been exhausted.

Bottom Up Merge Sort (Class with error)

package Exercise2;

import java.util.Queue; //for add etc m...cedure
import java.util.LinkedList;
public class BottomUpMergeSort{
    public static Queue sort(Double[] a)
    {
        int N = a.length;
        // Create a queue of the N queues,
        Queue> ques = new
        LinkedList>();//These two lines have an error
        for (int i = 0; i < N; i++)
        {
            // Each of the N queues contains a single
            // item
            Queue m = new LinkedList();
            m.add(a[i]);
            // add this single item queue to parent
            // queue
            ques.add(m);
        }
        // Repeat till queue of the N queues contains
        // a single queue.
        while (ques.size() > 1)
        {
            // apply the merging operation from 2.2.14
            Queue que1 = ques.remove();
            Queue que2 = ques.remove();
            Queue r = E2_2_14.merge(que1, que2);
            // insert the merged queue at the end of
            // parent queues.
            ques.add(r);
        }
        // remove and return the single sorted queue
        //left return
    }

    // print the array elements
    public static void printArr(Double[] a)
    {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "\t ");
            System.out.println();
        }
    }
}

MERGE SORTED CLASS

package Exercise2;

public class MergeSortedQueues{

    public static void main(String[] args) {
        Queue<Double> Q1 = new Queue<Double>();
        Queue<Double> Q2 = new Queue<Double>();

        Q1.enqueue(2.0);
        Q1.enqueue(4.0);
        Q1.enqueue(6.0);
        Q1.enqueue(8.0);

        Q2.enqueue(1.0);
        Q2.enqueue(3.0);
        Q2.enqueue(5.0);
        Q2.enqueue(7.0);

        //printQueue(Q1);
        //printQueue(Q2);

        Queue que=merge(Q1, Q2);
        printQueue(que);
    }

    public static Queue merge(Queue Q1, Queue Q2) {
        if(Q1.isEmpty()) return Q2;
        if (Q2.isEmpty())return Q1;

        Queue Q3=new Queue();

        int len1=Q1.size();
        int len2=Q2.size();

        int i=0, j=0;
        Comparable x,y;

        while(i<len1||j<len2) {
            if(i<len1&&j<len2) {
                x=(Comparable) Q1.peek();
                y=(Comparable) Q2.peek();

                if(less(x,y)) {
                    Q1.dequeue();
                    Q3.enqueue(x);
                    Q1.enqueue(x);
                    i++;

                }
                else {
                    Q2.dequeue();
                    Q3.enqueue(y);
                    Q2.enqueue(y);
                    j++;
                }
            }
            else if(i<len1&&j>=len2) {
                x=(Comparable)Q1.dequeue();
                Q1.enqueue(x);
                Q3.enqueue(x);
                i++;
            }
            else if (i>len1&&j<len2)
            {
                y=(Comparable) Q1.dequeue();
                Q2.enqueue(y);
                Q3.enqueue(y);
                j++;
            }
        }
        return Q3;

    }

    private static boolean less(Comparable x, Comparable y) {
        return x.compareTo(y)<0;
    }

    public static void printQueue(Queue que) {
        for(Object item:que) {
            System.out.println(item);
        }
    }
}

QUEUE INTERFACE

package Exercise2;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Queue<Item> implements Iterable<Item> {
    private Node<Item> first; // beginning of queue
    private Node<Item> last; // end of queue
    private int n; // number of elements on queue

    // helper linked list class
    private static class Node<Item> {
        private Item item;
        private Node<Item> next;
    }

    /**
     * Initializes an empty queue.
     */
    public Queue() {
        first = null;
        last = null;
        n = 0;
    }

    /**
     * Returns true if this queue is empty.
     *
     * @return {@code true} if this queue is empty; {@code false} otherwise
     */
    public boolean isEmpty() {
        return first == null;
    }

    /**
     * Returns the number of items in this queue.
     *
     * @return the number of items in this queue
     */
    public int size() {
        return n;
    }

    /**
     * Returns the item least recently added to this queue.
     *
     * @return the item least recently added to this queue
     * @throws NoSuchElementException if this queue is empty
     */
    public Item peek() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        return first.item;
    }

    /**
     * Adds the item to this queue.
     *
     * @param item the item to add
     */
    public void enqueue(Item item) {
        Node<Item> oldlast = last;
        last = new Node<Item>();
        last.item = item;
        last.next = null;
        if (isEmpty()) first = last;
        else oldlast.next = last;
        n++;
    }


    public Item dequeue() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        Item item = first.item;
        first = first.next;
        n--;
        if (isEmpty()) last = null; // to avoid loitering
        return item;
    }

    /**
     * Returns a string representation of this queue.
     *
     * @return the sequence of items in FIFO order, separated by spaces
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Item item : this) {
            s.append(item);
            s.append(' ');
        }
        return s.toString();
    }

    /**
     * Returns an iterator that iterates over the items in this queue in                                                  
   FIFO order.
     *
     * @return an iterator that iterates over the items in this queue in        
   FIFO order
     */
    public Iterator<Item> iterator() {
        return new ListIterator<Item>(first);
    }

    // an iterator, doesn't implement remove() since it's optional
    private class ListIterator<Item> implements Iterator<Item> {
        private Node<Item> current;

        public ListIterator(Node<Item> first) {
            current = first;
        }

        public boolean hasNext() { return current != null; }
        public void remove() { throw new UnsupportedOperationException(); }

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}

There's a syntax error that I can't work around.

1 个答案:

答案 0 :(得分:1)

The problem is in the following line(s):

Queue> ques = new LinkedList>();

The two >s in that line are causing the problem.

If you don't want to specify exactly which type of object is in the queue, do this:

Queue ques = new LinkedList();

Preferably, if you want to specific which type of object can be stored in the queue, do this:

Queue<Queue> ques = new LinkedList<Queue>();

... which can be shortened to:

Queue<Queue> ques = new LinkedList<>();