What's wrong with this Java Linked List representation of a stack of integers? -


okay, following code nullpointer exception in pop method. therefore, know 'head' must null when method runs. thing have no idea why , i've looked on code bit now. please help!

here is: 

node class:

public class stacknode{    private stacknode link; //link next node   private int value;    public stacknode(int value, stacknode linkvalue){     this.link = link;     this.value = value;   }   public stacknode(){    this.link = null;   }   public void setnodedata(int value){    this.value = value;    }   public void setlink(stacknode newlink){    this.link = newlink;    }   public int getvalue(){    return this.value;    }   public stacknode getlink(){    return link;    } } 

linked list class:

public class intstacklist{   private stacknode head;   public intstacklist(){ this.head = null; }  public void push(int value){    this.head = new stacknode(value, head);  }  public int pop(){    int value = this.head.getvalue(); //get int value stored in head node    this.head = head.getlink(); //sets head next node in line    return value;  } } 

i implementing in program converts decimal number binary(for class). can print data first node aka head of linked list have null issue when popping again.

you're assigning link in constructor if stacknode...

public class stacknode {      private stacknode link; //link next node     private int value;      public stacknode(int value, stacknode linkvalue) {         this.link = link;         this.value = value;     } 

it should

this.link = linkvalue; 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -