algorithm - Java Queue Implementation with Pointers -


i need understanding this. know how implement queue there 1 small part bothering me. drew on notebook flow of how things work don't how head has nextnode without me setting it. how head end pointing next node?

public void enqueue(t data){         count++;         node<t> current = tail;         tail = new node<>(data);         if(isempty()) {             ///////////////////////////////////////////////////////////////////////////////             // when runs, doesn't head.getnextnode point null?             // if list empty, tail null.             // on deque method, can sout head.getnextnode() , data back, how?             ///////////////////////////////////////////////////////////////////////////////             head = tail;          } else {             current.setnextnode(tail);         }     } 

below, dequeing works fine, think i'm having issue understanding whole reference/pointer thing

public t dequeue() {         if(isempty()) {             return null;         }         count--;         t datatoremove = head.getdata();         /////////////////////-+{[update]}+-////////////////////////////////         // head next node from? works,         // next node coming i'm asking?         ///////////////////////////////////////////////////////////////////         head = head.getnextnode();          return datatoremove;     } 

i figured out:

when list empty, head points tail then, when enqueue method gets called again, current pointing tail, head still pointing tail reference head pointing same reference current in else statement, current sets next node same reference head pointing to. that's how head gets nextnode set. when method runs again, current point reference again head still pointing 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);             }         } 

Comments