java - Is there a mistake in this illustration? -
in chapter 14.2, page 620, in "big java" (international 4th edition), cay horstmann, shown how implement linked list. add method of listiterator looks this:
public void add(object element) { if(position == null) { addfirst(element); position = first; } else { node newnode = new node(); newnode.data = element; newnode.next = position.next; position.next = newnode; position = newnode; } previous = position; }
the corresponding illustration looks 1 added below. first 1 before "newnode" has been added, , second 1 after. can tell me if there mistake in second picture. shouldn't fields "previous" , "position" point same object after call "add(object element)"? top picture before , bottom 1 after insertion.
the way see code seems wrong , previous
should have been set position
before changed.
something this:
public void add(object element) { previous = position; if(position == null) { addfirst(element); position = first; } else { node newnode = new node(); newnode.data = element; newnode.next = position.next; position.next = newnode; position = newnode; } }
that match intended outcome of second picture
Comments
Post a Comment