在 Java 中使用 toString 和队列

Working with a toString and a queue in Java

我已经切换了 toString 中的所有内容以尝试获得我想要的结果,但我没有运气。我试图得到这个结果:

QUEUE TESTING

3

7

7

4

The size of the queue is: 3

The queue contains:

4 9 8 

但是,我得到的是:

QUEUE TESTING
3
7
7
4
The size of the queue is: 3
The queue contains:
 9 8

而且我不明白为什么没有将 4 与 9 和 8 一起返回。有人可以告诉我发生了什么事吗?谢谢!

这是我正在使用的代码(附加文件被遗漏):

public class Murray_A06Q1 {

    public static void main(String[] args) {

        LinkedQueue<Integer> queue = new LinkedQueue<Integer>();
        System.out.println("QUEUE TESTING");

        queue.enqueue(3);
        queue.enqueue(7);
        queue.enqueue(4);
        System.out.println(queue.first());
        queue.dequeue();        
        queue.enqueue(9);
        queue.enqueue(8);
        System.out.println(queue.first());        
        System.out.println(queue.dequeue());
        System.out.println(queue.first());        

        System.out.println("The size of the queue is: " + queue.size());
        System.out.println("The queue contains:\n" + queue.toString());        
    }


  public static class LinkedQueue<T> implements QueueADT<T> {
    private int count;
    private LinearNode<T> head, tail; //front, back

    // Constructor      
    public LinkedQueue() {
        count = 0;
        head = tail = null;
    }

    // Adds the specified element to the tail of this queue.   
    public void enqueue(T element) {

        LinearNode<T> node = new LinearNode<T>(element);

        if (isEmpty())
            head = node;
        else
            tail.setNext(node);

        tail = node;
        count++;
    }

    //Removes the element at the head of this queue and returns a
    public T dequeue() throws EmptyCollectionException {
        if (isEmpty())
            throw new EmptyCollectionException("queue");

        T result = head.getElement();
        head = head.getNext();
        count--;

        if (isEmpty())
            tail = null;

        return result;
    } // End of dequeue

    // first() throws EmptyCollectionException         
    public T first() throws EmptyCollectionException {
        return head.getElement();
    }

    // Beginning of isEmpty()           
    public boolean isEmpty() {
        if(count == 0){
            return true;
        }   
        else {
            return false;           
        }
    } // End of isEmpty()

    // Beginning of size()          
    public int size() {
        return count;
    }

    // Begin of toString() method           
    public String toString() {

        if (isEmpty()) {
            return " ";
        }

        StringBuilder sb = new StringBuilder();
        LinearNode<T> next = head.getNext();

        while(next != null){
            sb.append(" ").append(next.getElement());
            next = next.getNext();
        }
        return sb.toString();   
    } // End of toString method

  }

}

您从第二个元素开始。在 toString() 中,您使用 head.getNext() 初始化 next,其中 head 是第一个元素,head.next() 是第二个元素。这会导致输出跳过第一个元素,如果队列中只有一个元素,这将导致 NullPointerException

next 初始化为 head

您需要像这样编写 toString 方法:

public String toString() {

    if (isEmpty()) {
        return " ";
    }

    StringBuilder sb = new StringBuilder();
    LinearNode<T> next = head;

    while(next != null){
        sb.append(" ").append(next.getElement());
        next = next.getNext();
    }

    return sb.toString();

} // End of toString method

toString 从未打印 HEAD 值,因为您在 while 循环之前将 head.getNext 分配给 next。