Linked List and Double Linked List -
as part of university work need understand linked list code have been given. having problems insert method-the part deals general case-as code below:
//in general case, need chain down linked list //from head until find location new //list node. if reach end of list before finding //the specified location, know given index out //of range , throw exception. else { node nodepointer = listhead; int = 1; while (i < index) { nodepointer = nodepointer.next; += 1; if (nodepointer == null) { throw new sequencelistexception("indexed element out of range"); } } //now we've found node before position of //new one, 'hook in' new node. nodepointer.next = new node(o, nodepointer.next);
the problem having can see method inserts node , makes point next node, node replaced. far can see there has been no change node replaced, meaning there 2 nodes pointing same next.node. mis understanding this, or happens? if so, not seem logical me there ae 2 nodes pointing single node. if not case, @ point in code change pointer previous node
the whole code below:
class sequencelistexception extends exception { sequencelistexception() { super(); } sequencelistexception(string s) { super(s); } } /** * <dl> * <dt>purpose: implementation of sequence adt. * <dd> * * <dt>description: * <dd>this class implementation of sequence using linked list * underlying data structure. capacity therefore unlimited , * overflow not need checked. * </dl> * * @author danny alexander * @version $date: 2000/01/08 */ public class sequencelist { /** * member class node encapsulates nodes of linked list in * stack stored. each node contains data item , * reference node - next in linked list. */ protected class node { public node(object o) { this(o, null); } public node(object o, node n) { datum = o; next = n; } //the node data structure consists of 2 object references. //one datum contained in node , other //the next node in list. protected object datum; protected node next; } //we use object references head , tail of list (the head //and tail of sequence, respectively). private node listhead; private node listtail; //only require single constructor, sets both object //references null. /** * constructs empty sequence object. */ public sequencelist() { listhead = null; listtail = null; } /** * adds new item @ start of sequence. */ public void insertfirst(object o) { //there special case when sequence empty. //then both head , tail pointers needs //initialised reference new node. if (listhead == null) { listhead = new node(o, listhead); listtail = listhead; } //in general case, add new node @ start //of list via head pointer. else { listhead = new node(o, listhead); } } /** * adds new item @ end of sequence. */ public void insertlast(object o) { //there special case when sequence empty. //then both head , tail pointers needs //initialised reference new node. if (listhead == null) { listhead = new node(o, listhead); listtail = listhead; } //in general case, add new node end //of list via tail pointer. else { listtail.next = new node(o, listtail.next); listtail = listtail.next; } } /** * adds new item @ specified position in sequence. */ public void insert(object o, int index) throws sequencelistexception { //check index positive. if (index < 0) { throw new sequencelistexception("indexed element out of range"); } //there special case when sequence empty. //then both head , tail pointers needs //initialised reference new node. if (listhead == null) { if (index == 0) { listhead = new node(o, listhead); listtail = listhead; } else { throw new sequencelistexception("indexed element out of range"); } } //there special case insertion @ head of //the sequence. else if (index == 0) { listhead = new node(o, listhead); } //in general case, need chain down linked list //from head until find location new //list node. if reach end of list before finding //the specified location, know given index out //of range , throw exception. else { node nodepointer = listhead; int = 1; while (i < index) { nodepointer = nodepointer.next; += 1; if (nodepointer == null) { throw new sequencelistexception("indexed element out of range"); } } //now we've found node before position of //new one, 'hook in' new node. nodepointer.next = new node(o, nodepointer.next); //finally need check tail pointer //correct. special case occurs if new //node inserted @ end, in case, need //to update tail pointer. if (nodepointer == listtail) { listtail = listtail.next; } } } /** * removes item @ start of sequence. */ public void deletefirst() throws sequencelistexception { //check there in sequence delete. if (listhead == null) { throw new sequencelistexception("sequence underflow"); } //there special case when there 1 item in //sequence. both pointers need reset null. if (listhead.next == null) { listhead = null; listtail = null; } //in general case, unlink first node of //list. else { listhead = listhead.next; } } /** * removes item @ end of sequence. */ public void deletelast() throws sequencelistexception { //check there in sequence delete. if (listhead == null) { throw new sequencelistexception("sequence underflow"); } //there special case when there 1 item in //sequence. both pointers need reset null. if (listhead.next == null) { listhead = null; listtail = null; } //in general case, need chain way down //list in order reset link of second last //element null. else { node nodepointer = listhead; while (nodepointer.next != listtail) { nodepointer = nodepointer.next; } //unlink last node , reset tail pointer. nodepointer.next = null; listtail = nodepointer; } } /** * removes item @ specified position in sequence. */ public void delete(int index) throws sequencelistexception { //check there in sequence delete. if (listhead == null) { throw new sequencelistexception("sequence underflow"); } //check index positive. if (index < 0) { throw new sequencelistexception("indexed element out of range"); } //there special case when there 1 item in //sequence. both pointers need reset null. if (listhead.next == null) { if (index == 0) { listhead = null; listtail = null; } else { throw new sequencelistexception("indexed element out of range."); } } //there special case when first element has //be removed. else if (index == 0) { deletefirst(); } //in general case, need chain down list find //the node in indexed position. else { node nodepointer = listhead; int = 1; while (i < index) { nodepointer = nodepointer.next; += 1; if (nodepointer.next == null) { throw new sequencelistexception("indexed element out of range"); } } //unlink node , reset tail pointer if //node last one. if (nodepointer.next == listtail) { listtail = nodepointer; } nodepointer.next = nodepointer.next.next; } } /** * returns item @ start of sequence. */ public object first() throws sequencelistexception { if (listhead != null) { return listhead.datum; } else { throw new sequencelistexception("indexed element out of range"); } } /** * returns item @ end of sequence. */ public object last() throws sequencelistexception { if (listtail != null) { return listtail.datum; } else { throw new sequencelistexception("indexed element out of range"); } } /** * returns item @ specified position in sequence. */ public object element(int index) throws sequencelistexception { //check index positive. if (index < 0) { throw new sequencelistexception("indexed element out of range"); } //we need chain down list until reach indexed //position node nodepointer = listhead; int = 0; while (i < index) { if (nodepointer.next == null) { throw new sequencelistexception("indexed element out of range"); } else { nodepointer = nodepointer.next; += 1; } } return nodepointer.datum; } /** * tests whether there items in sequence. */ public boolean empty() { return (listhead == null); } /** * returns number of items in sequence. */ public int size() { //chain down list counting elements node nodepointer = listhead; int size = 0; while (nodepointer != null) { size += 1; nodepointer = nodepointer.next; } return size; } /** * empties sequence. */ public void clear() { listhead = null; listtail = null; } }
this line of code takes care of both creating new node, pointing next link, , making previous node point new node:
nodepointer.next = new node(o, nodepointer.next);
first, new node(o, nodepointer.next)
creates new node , point next node in line. reference newly created node assigned nodepointer.next
.
to clarify, asked:
"as far can see there has been no change node replaced"
nodepointer
reference node directly before index, nodepointer.next
node being "replaced".
Comments
Post a Comment