-1

I need help understanding this. I know how to implement the queue but there is one small part bothering me. I drew on my notebook the flow of how things work but I don't get how the head has a nextNode without me setting it. How does the head end up pointing to the next node?

public void enqueue(T data){
        count++;
        Node<T> current = tail;
        tail = new Node<>(data);
        if(isEmpty()) {
            ///////////////////////////////////////////////////////////////////////////////
            // when this runs, doesn't head.getNextNode point to null?
            // if the list is empty, then tail is null.
            // On the deque method, I can sout head.getNextNode() and I get data back, how?
            ///////////////////////////////////////////////////////////////////////////////
            head = tail;

        } else {
            current.setNextNode(tail);
        }
    }

Below, the dequeing works fine, I think I'm having an issue understanding the whole reference/pointer thing

public T dequeue() {
        if(isEmpty()) {
            return null;
        }
        count--;
        T dataToRemove = head.getData();
        /////////////////////-+{[UPDATE]}+-////////////////////////////////
        // WHERE DOES HEAD GET THE NEXT NODE FROM? THIS WORKS, BUT WHERE IS
        // THE NEXT NODE COMING FROM IS WHAT I'M ASKING?
        ///////////////////////////////////////////////////////////////////
        head = head.getNextNode();

        return dataToRemove;
    }
franklinexpress
  • 1,149
  • 14
  • 44
  • 1
    Java doesnt support pointers. Any time you create an object in Java, you actually creating a pointer to the object, this pointer could then be set to a different object or null – nano_nano Jan 29 '16 at 09:28
  • @StefanBeike there is reference in java http://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java – franklinexpress Jan 29 '16 at 09:29
  • so you are trying to ask how you get the next node from head even if that node is not present.?? – Mr. Noddy Jan 29 '16 at 09:30
  • I'm asking Why can I get nextnode without setting it. where is the nextnode being set? the code works without me calling head.setNextNode()... – franklinexpress Jan 29 '16 at 09:32
  • 1
    "Java doesn't support pointers" -- Java is full of pointers. Every handle on an Object is a pointer. It doesn't support all the same pointer operations as C++ but it certainly supports pointers. – khelwood Jan 29 '16 at 09:32
  • see updated comment in dequeuer method – franklinexpress Jan 29 '16 at 09:35
  • I'm not sure I understand your question. Maybe `head` doesn't have `nextNode` set to anything. Maybe it's null. Fields that have never been set to anything hold the value null. – khelwood Jan 29 '16 at 09:39
  • Yes, but the value IS NOT null, I tested. Anyways, I figured it out. – franklinexpress Jan 29 '16 at 09:49

1 Answers1

0

I figured it out:

When the list is empty, the head points to tail then, when the enqueue method gets called again, current will be pointing to tail, but head will still be pointing to tail reference so now head is pointing to the same reference as current in the else statement, current sets next node to the same reference the head is pointing to. That's how the head gets its nextNode set. When the method runs again, current will point to another reference again but head will still be pointing to its original reference. BAM

 public void enqueue(T data){
            count++;
            Node<T> current = tail; 
            tail = new Node<>(data);
            if(isEmpty()) {
                head = tail;

            } else {
                current.setNextNode(tail);
            }
        }
franklinexpress
  • 1,149
  • 14
  • 44