Implementation of iterators in JAVA list __java

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.