Confusion over how Java's Garbage Collector works (Nodes + Queue) -


this question has answer here:

so been trying implement linkedlist, stack, queue in java.

for each 1 i'm using node class such that, don't want discuss how implementation since aware there better ways it, want focus on question.

public class node<e> {     private e data;     private node<e> next;     private node<e> prev;      public node(e data) {         this.data = data;         this.next = null;         this.prev = null;     }      public e getdata() {         return this.data;     }      public node<e> getnext() {         return this.next;     }      public node<e> getprev() {         return this.prev;     }      public void setprev(node<e> prev) {         this.prev = prev;     }      public void setdata(e data) {         this.data = data;     }      public void setnext(node<e> next) {         this.next = next;     } } 

now node class there, keep getting mixed on how garbage collector works, lets queue class

public class queue<e> {     private int size;     private node<e> head, tail;      public queue() {         this.size = 0;         this.head = this.tail = null;     }      public queue(e data) {         node<e> temp = new node<e>(data);         this.tail = this.head = temp;         this.size = 0;     }      public boolean enqueue(e data) {         node<e> temp = new node<e>(data);          if (this.head == null) {             this.tail = temp;             this.head = temp;         } else {             temp.setnext(this.head);             this.head.setprev(temp);             this.head = temp;         }         this.size++;         return true;     }      public e dequeue() {         if (this.tail == null)             throw new indexoutofboundsexception();         else {             e data = this.tail.getdata();             this.tail.setprev(null);             this.tail = temp;             this.tail.setnext(null);             this.size--;             return data;         }     }      public int getsize() {         return this.size;     }      public e peak() {         if (this.tail == null)             throw new indexoutofboundsexception();         else             return this.tail.getdata();     }      public boolean contains(e data) {         if (this.head == null)             return false;         else {             (node<e> cursor = this.head; cursor != null; cursor = cursor                     .getnext()) {                 if (cursor.getdata().equals(data))                     return true;             }         }         return false;     } } 

now getting how garbage collector works confused. have heard, clean references don't pointed too. keep getting nullpointerexception on dequeue class on part

 this.tail.setnext(null); 

now, hearing garbage collector work, nothing can reference it, thought myself nodes set this

       head          tail  null<-[1]-><-[2]-><-[3]->null 

where each node can point next , previous, dequeue think have few things

1) data (that easy)

2) temp node points previous

 node<e> temp = this.tail.getprev() 

3) here start lost, in order each node no longer referenced, have rid of things pointer it, means must set null

this.tail.setprev(null); 

since when delete node after that, can't go backwards erase reference

       head               tail  null<-[1]-><-[2]-> null<-[3]->null  <-[temp]-> ( equals node [2]) 

4) set tail point @ temp node, prev node was

this.tail = temp; 

no should this

       head   tail      null<-[1]-><-[2]->(this still points [3])    null<-[3]->null 

5) second node still points memory address of [3], continue

this.tail.setnext(null);  

in order make nothing @ references spot of memory no longer in us,

       head   tail         deleted gc  null<-[1]-><-[2]->null      null<-[3]->null 

however, part gives me nullpointerexception when there 1 node left in queue.

now, know may wrong on lot of this, still learning, jsut not sure how stuff have each node make sure garbage collector gets do, need set both prev , next null? or one? etc, appreciated, thank ;)

there bug in code. has nothing garbage collector. nullpointerexception because this.tail null in example when have 1 node in queue. assign temp = this.tail.getprev(); null 1 node only. assign this.tail = temp;. below find right implementation of dequeue(). don't have to, maybe people consider practice set null in deleted node.

public e dequeue() {         if (this.tail == null)             throw new indexoutofboundsexception();         else {             e data = this.tail.getdata();             node<e> temp = this.tail;              this.tail = temp.getprev();             if ( this.tail == null ) { // if last node                 this.head = null;                 return data;             }             this.tail.setnext(null);              temp.setprev(null);             temp.setnext(null);              this.size--;             return data;         }     } 

in method enqueue() check head emtpy queue. in method dequeue() check tail same. might little confusing. should check both null. it's additional test of program.

there bug in constructor. this.size should set 1 not 0.

public queue(e data) {         node<e> temp = new node<e>(data);         this.tail = this.head = temp;         this.size = 1;     } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -