java - int cannot be converted to Node -
i'm getting error:
incompatible types: int cannot converted node
why getting kind of error?
public void insertitem(int e) { if(e<head.getitem()) { addfirst(e); } else if(count==1) { addlast(e); } else { node newnode = new node(e); node prev = head; node p = prev.getitem(); while(p!=null) { if(e<p.getitem()) { newnode.setlink(p); prev.setlink(newnode); p = p.getlink(); prev = prev.getlink(); } } } } public class node { private node link; private int item; public node(int item, node link) { setlink(link); setitem(item); } public node(int item) { this(item, null); } public node() { this(0, null); } //empty node public void setlink(node link) { if (link != null) this.link = link; } public void setitem(int item) { this.item = item; } public node getlink() { return link; } public int getitem() { return item; } public string tostring() { return item + ""; } }
i using getitem() class here is:
public class node { private node link; private int item; public node(int item, node link) { setlink(link); setitem(item); } public node(int item) { this(item, null); } public node() { this(0, null); } //empty node public void setlink(node link){ if(link != null) this.link = link; } public void setitem(int item) // {this.item = item;} public node getlink(){return link;} public int getitem(){return item;} public string tostring(){return item+ "";} }
as can see, node#getitem
returns int
value not node
value.
so when trying write : node p = prev.getitem();
showing error.
maybe need : node p = prev.getlink();
Comments
Post a Comment