简体   繁体   English

创建Queue类并从头到尾打印里面的元素

[英]Create the Queue class and print the element inside from first to last

I currently have an assignment that I need to create my own Queue class and methods such as enqueue(), dequeue(), and display the elements from first to last.我目前有一个任务,我需要创建自己的队列类和方法,例如 enqueue()、dequeue(),并从头到尾显示元素。 This is what I did so far:这是我到目前为止所做的:

The node class:节点类:

  class Node{
        //attributes
        public String data;
        public 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;
        }
    }

This is my queue class:这是我的队列类:

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.next = this.rear;
    this.rear = node;
}

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

//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){
        System.out.println("Queue is empty");
    } else{
        Node tmp = this.rear;
        while(tmp != null) {
            System.out.print(tmp.data + " ");
            tmp =tmp.next;
        }
        System.out.println();
    }
}
}

And the main class for testing:以及用于测试的主要类:

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

So what I want the output to be is所以我想要的输出是

1 2 3

However, my output is :但是,我的输出是:

3 2 1

Can you guys have a look, I think it must be something to do with the displayQueue() method but I don't know how to fix it, can you guys help me.你们能不能看看,我想应该是displayQueue()方法的问题,但是我不知道怎么解决,你们能不能帮帮我。 Thanks a lot非常感谢

I believe your enqueue logic for the most part is correct in the sense that you are adding new elements at the end of the queue and resetting this.rear to the new element added.我相信您的入enqueue逻辑在很大程度上是正确的,因为您在队列末尾添加新元素并将this.rear重置为添加的新元素。 However, you should make your displayQueue method recursive so that you can follow the FIFO, first-in-first-out, principle and iterate through the queue from the start to the end instead of what you have now, which is iterating through the queue from the end to the start.但是,您应该使displayQueue方法递归,以便您可以遵循 FIFO 先进先出原则并从头到尾遍历队列,而不是您现在拥有的遍历队列的方法从结束到开始。 The reason that recursion is recommended here is that you structured your queue currently in a way where there is only a one-way route from the last node to the first node and not vice versa, so your base condition for the recursive method can be when the node your iterating on is null .此处推荐递归的原因是,您当前以一种方式构建队列,其中从最后一个节点到第一个节点只有一条单向路由,反之亦然,因此递归方法的基本条件可以是您迭代的节点是null At that point you can start printing out nodes as you back-track from the start to the end of the queue.在这一点上,您可以开始打印节点,因为您从队列的开始到结束回溯。 Note that the next pointer of the node you are iterating on will be null when that node you are iterating on is the start of the queue.请注意,当您正在迭代的节点是队列的开始时,您正在迭代的节点的next指针将为null Here's what a recursive displayQueue method would look like with a helper function,这是带有辅助函数的递归displayQueue方法的样子,

public void displayQueue(){
    if (this.rear == null) {
        System.out.println("Queue is empty");
    } 
    else {
        displayQueueHelper(this.rear);
    }
}

private void displayQueueHelper(Node n) {
    if (n == null) {
        return;
    }
    displayQueueHelper(n.next);
    System.out.print(n.data + " ");
}

I chose to use a recursive helper function because you should still use the main outside displayQueue function to check if the queue is empty before deciding whether or not to iterate through the queue.我选择使用递归辅助函数,因为在决定是否遍历队列之前,您仍然应该使用 main 外部displayQueue函数来检查队列是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM