Java 使用您自己的入队和出队方法反转队列元素

Java 使用您自己的入队和出队方法反转队列元素,java,queue,Java,Queue,我正在做一个任务,我需要创建自己的队列类,我创建了它,但是,现在我想反转队列中的元素,我尝试实现自己的reverseQueue()方法,但我一直收到NullPointerException错误,你们看一下,这是我到目前为止的代码: 节点类: class Node{ //attributes private String data; private Node next; //basic constructor Node(){ } Nod

我正在做一个任务,我需要创建自己的队列类,我创建了它,但是,现在我想反转队列中的元素,我尝试实现自己的reverseQueue()方法,但我一直收到NullPointerException错误,你们看一下,这是我到目前为止的代码:

节点类:

class Node{
    //attributes
    private String data;
    private Node next;

    //basic constructor
    Node(){

    }

    Node(String data){
        this.data = data;
        this.next = null;
    }

    //accessors
    public String getData(){
        return this.data;
    }
    public Node getNext(){
        return this.next;
    }

    //mutators
    public void setData(String tmpData){
        this.data = tmpData;
    }
    public void setNext(Node tmpNext){
        this.next = tmpNext;
    }

    //method to print the data
    public void printNode(){
        if(this.data ==  null){
            System.out.println("NO DATA");
        } else{
            String tmp = this.data;
            while(tmp != null){
                System.out.println(tmp);
            }
        }
    }
}
类MyQueue

class MyQueue{
    //attributes
    private Node front, rear;

    MyQueue(){
        this.front = null;
        this.rear = null;
    }

    //method to insert one node at the end of the queue
    public void enqueue(Node node){
        node.setNext(this.rear);
        this.rear = node; 
    }

    //get and remove the front node from queue
    public Node dequeue(){
        Node next = this.rear.getNext();
        //check if the queue empty or not
        if(this.rear == null){
            System.out.println("Queue is empty");
            return null;
        } else if(next == null){ // the queue only have 1 element
            this.rear = null;
            return this.rear;
        } else{ //remove the front node
            Node tmp = this.rear;
            //traverse to reach the second element
            while (tmp.getNext().getNext() != null) {
                tmp = tmp.getNext();
            }
            //remove first element
            tmp.setNext(null);
            return this.rear;
        }
    }

    //check if the queue is empty or not
    public boolean isEmpty(){
        if(this.rear == null){
            return true;
        } else{
            return false;
        }
    }


    //method to display
    public void displayQueue(){
        if (this.rear == null) { //check if the queue is empty
            System.out.println("Queue is empty");
        } 
        else {
            displayQueueHelper(this.rear); //call the helper method
        }
    }

    private void displayQueueHelper(Node n) {
        if (n == null) //base case until reach the null value
            return;     
        displayQueueHelper(n.getNext()); //recursively call and print the data
        System.out.print(n.getData() + " ");
    }
}
我的主类,在其中创建reverseQueue()方法

MyQueue类中的Node类和方法中的所有内容都非常好,我想我在reverseQueue()方法中犯了一些错误,你们能看一下吗,非常感谢

import java.util.*;
class Main{
    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.enqueue(new Node("1"));
        queue.enqueue(new Node("2"));
        queue.enqueue(new Node("3"));
        queue.enqueue(new Node("4"));
        queue.enqueue(new Node("5"));
        queue.displayQueue();
        System.out.println();
        reverseQueue(queue);
        queue.displayQueue();   
    }

    //question 6: reverse Queue
    public static MyQueue reverseQueue(MyQueue queue){
        if(queue.isEmpty())
            return queue;
        Node data = queue.dequeue();
        reverseQueue(queue);
        queue.enqueue(data);
        return queue;       
    }
}