java - Why is it not enough to change just nextNode in this data structure? -
java - Why is it not enough to change just nextNode in this data structure? -
in next info structure
public class listnode<t> { t data; listnode<t> nextnode; listnode(t object) { this(object, null); } listnode(t object, listnode<t> node) { info = object; nextnode = node; } public t getdata() { homecoming data; } public listnode<t> getnextnode() { homecoming nextnode; } }
public class list<t> { private listnode<t> firstnode; private listnode<t> lastnode; private string name; public list() { this("list"); } public list(string listname) { name = listname; firstnode = lastnode = null; } public void insertatfront(t insertitem) { if(isempty()) firstnode = lastnode = new listnode<t>(insertitem); else firstnode = new listnode<t>(insertitem, firstnode); } public void insertatback(t insertitem) { if(isempty()) firstnode = lastnode = new listnode<t>(insertitem, null); else lastnode = lastnode.nextnode = new listnode<t>(insertitem) } }
why not sufficient just
else lastnode.nextnode = new listnode<t>(insertitem)
in insertatback method? (i not sure a = b = c = something
mean in java.)
it's not sufficient this:
else lastnode.nextnode = new listnode<t>(insertitem)
because list
needs maintain track of first , lastly nodes. when insert @ back, must update lastnode
, otherwise lastnode
refer "old" lastly node, second-to-last.
if list empty, neither first nor lastly node set anything; must initialized.
the a = b = c = something
syntax assigns lastly value/expression variables not last, associating right-to-left, e.g. something
gets assigned c
, b
, , a
.
java data-structures
Comments
Post a Comment