From Java data structure and algorithm book
If the node is inserted, current points to the inserted element, and if the node is deleted, then current=current.next;
Here's the complete code:
Package com.xll; public class Interiterapp {public static void main (string[] args) {singlelinklist singlelinklist = new Singlelink
List ();
Iteratorlist iteratorlist = Singlelinklist.getiteratorlist ();
Iteratorlist.insertcurrentafter (2);
Iteratorlist.insertcurrentbefore (4);
Iteratorlist.insertcurrentafter (10);
Iteratorlist.insertcurrentafter (15);
Iteratorlist.insertcurrentafter (50);
Iteratorlist.insertcurrentbefore (25);
Singlelinklist.displaylink ();
System.out.println ("=====================================");
Iteratorlist.deletecurrent ();
Singlelinklist.displaylink ();
}//End Class Interiterapp class linkforth{public int data;
Public Linkforth Next;
-----------------------------------------------------public Linkforth (int data) {this.data = data; }//-----------------------------------------------------public void DisplayLink () {System.out.println ("Data:" +
data); }
//--------------------------------------------------------------}//End Class Linkforth class singlelinklist{private Linkforth;
----------------------------------------------------------------------public singlelinklist () {i = null;
}//-------------------------------------------------------------------public Linkforth GetFirst () {return i; }//----------------------------------------------------------------public void Setfirst (Linkforth f) {A
F }//-----------------------------------------------------------------public boolean IsEmpty () {if (i = null)
{return true;
return false;
}//-----------------------------------------------------------------------public iteratorlist getiteratorlist () {
Return to New Iteratorlist (this); }//--------------------------------------------------------------public void DisplayLink () {Linkforth current =
First
while (current!= null) {Current.displaylink ();
current = Current.next;
}//End While}
--------------------------------------------------------------}//End Class Singlelinklist class iteratorlist{pri
Vate Linkforth Previous;
Private Linkforth current;
Private Singlelinklist ourlist;
------------------------------------------------------public iteratorlist (Singlelinklist singlelinklist) {
Ourlist = singlelinklist;
}//----------------------------------------------------------public void Reset () {current = Ourlist.getfirst ();
previous = null; }//----------------------------------------------------whether/** iteration to the tail/public boolean atEnd () {if (= null
) {return true;
return false;
}//------------------------------------------/** iteration to the next node/public void Nextlink () {previous = current;
current = Current.next;
//----------------------------------------------/** Get the current node/public Linkforth GetCurrent () { }//----------------------------------------------public void insertcurrentAfter (int data) {Linkforth Insertnode = new Linkforth (data);
if (Ourlist.isempty ()) {Ourlist.setfirst (Insertnode);
current = Insertnode;
}else {insertnode.next = Current.next;
Current.next = Insertnode; Nextlink (); Point to next node}//--------------------------------------------------------------public void Insertcurrentbefore
T data) {Linkforth Insertnode = new Linkforth (data);
if (Ourlist.isempty ()) {Ourlist.setfirst (Insertnode);
current = Insertnode;
}else {if (previous!= null) {insertnode.next = Previous.next; Previous.next = Insertnode;
Previous node invariant current = Insertnode;
}else {insertnode.next = Ourlist.getfirst ();
Ourlist.setfirst (Insertnode);
Reset (); }//end Else}//--------------------------------------------------------------------------public int Deletecu
Rrent () {int deletevalue = Current.data;
if (previous = = null) {Ourlist.setfirst (current.next);
Reset (); }else {previous.next = current.next;
current = Current.next;
if (atEnd ()) reset ();
else current = Current.next;
}//end else return deletevalue;
}//------------------------------------------------------------------}//End Class Iteratorlist