1

I wrote the following implementation of the queue using a linked list that does not maintain a reference to the tail node. When I try to print the queue, it outputs only the head i.e. only one node. What is the error? Thanks in advance!

package DataStructures;

import java.util.Scanner;

class Node {
    int x;
    Node nextNode;

    public Node(int x) {
        this.x = x;
        nextNode = null;
    }
}

class Queue {
    Node head = null;
    int n = 0;

    public void enqueue(int x) {
        if (n==0){
            head = new Node(x);
            n++;
            return;
        }
        Node tempHead = head;
        while (tempHead != null){
            tempHead = tempHead.nextNode;
        }
        tempHead = new Node(x);
        tempHead.nextNode = null;
        n++;
    }

    public int dequeue() {
        if (head == null) {
            throw new Error("Queue under flow Error!");
        } else {
            int x = head.x;
            head = head.nextNode;
            return x;
        }
    }

    public void printTheQueue() {
        Node tempNode = head;
        System.out.println("hi");
        while (tempNode != null){
            System.out.print(tempNode.x + "  ");
            tempNode = tempNode.nextNode;
        }

    }

}

public class QueueTest {

    private static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        Queue queue = new Queue();
        while (true){
            int x = in.nextInt();
            if (x == -1){
                break;
            } else{
                queue.enqueue(x);
            }
        }

        queue.printTheQueue();
    }

}
2
  • When you enqueue, there is nothing that connects between the head and your new node. Commented Aug 15, 2015 at 10:59
  • @RealSkeptic But I do have a temporary reference to head and propagate through it to reach the last node. Once I reach the last node, I point its nextNode to new node with data key x. Commented Aug 15, 2015 at 11:02

2 Answers 2

2

You never assign a node to nextNode, so your list is either empty or consists of one node.

Here is a solution:

public void enqueue(int x) {
    n++;
    if (head == null) {
        head = new Node(x);
    else {
        Node last = head;
        while (last.nextNode != null)
            last = last.nextNode;
        last.nextNode = new Node(x);
    }
}

Technically you don't need n but you could use it as cache for the size of the list. And you should decrease it in deque().

Sign up to request clarification or add additional context in comments.

3 Comments

I would be thankful if you tell me what should be the change in the code
Thanx... got it... I was assigning new Node() to tempHead which was a flying reference.. instead I should have assigned new Node() to tempHead.nextNode with proper logic!!!
The n++ should come at the end of the method.
0

Make your enqueue to this:

public void enqueue(int x) {
 if (n==0){
        head = new Node(x);
        head.nextNode=null;
        n++;
        return;
    }
    Node tempHead = head;
    while (tempHead.nextNode!= null){
        tempHead = tempHead.nextNode;
    }
    Node newNode = new Node(x);
    tempHead.nextNode=newNode;
    newNode.nextNode = null;
    n++;
}

1 Comment

Yes its correct!......but if I remove the if statement if(n==0), your code would crash since head is null and we are accessing its nextNode.... Can You provide a hint for this??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.