gpt4 book ai didi

java - 自下而上合并排序代码的语法错误。我该如何解决?

转载 作者:行者123 更新时间:2023-12-02 09:59:50 26 4
gpt4 key购买 nike

此代码中有语法错误,我不知道如何让它运行。这是 Chegg 教科书解决方案“算法(第 4 版)”第 2.2 章问题 14E 和 15E 中的代码。

以下行(第一类的第 10 行和第 11 行)导致了错误:

队列> ques = new

LinkedList>();

所有快速建议均已用尽。

自下而上合并排序(有错误的类)

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();
}
}
}

合并已排序的类

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);
}
}
}

队列接口(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;
}
}
}

存在一个我无法解决的语法错误。

最佳答案

问题出在以下几行:

Queue> ques = new LinkedList>();

该行中的两个 > 导致了问题。

如果您不想准确指定队列中的对象类型,请执行以下操作:

Queue ques = new LinkedList();

如果您想指定队列中可以存储哪种类型的对象,最好执行以下操作:

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

...可以缩短为:

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

关于java - 自下而上合并排序代码的语法错误。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com