Skip to content
Advertisement

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. 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. Thanks a lot

Advertisement

Answer

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. 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. 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. 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. Here’s what a recursive displayQueue method would look like with a helper function,

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement