Java bidirectional linked list Realization __java

Source: Internet
Author: User
public class Doublylinklist {private class data{private Object obj;
		Private Data left = null;
		
		Private Data right = NULL;
		Data (Object obj) {this.obj = obj;
	} private Data a = null;
	
	Private Data last = null; public void Insertfirst (Object obj) {Data data = new data (obj);//if (A/= NULL) {//I = data;//last = da
Ta  
			}else{//Data.right = A///first.left = data;///////Refactor code if (A/= null) {  
		last = data;  
			}else{data.right = A;  
		First.left = data;  
	} a = data; public void Insertlast (Object obj) {Data data = new data (obj);//if (A/= NULL) {//I = data;//Last
= data;  }else{//last.right = data;//Data.left = last;//last = data;//}//Refactor code if (A/= null) {A
		= data;
			}else{last.right = data;
		Data.left = Last;
	last = data; public boolean InsertAfter (Object target,object obj) {Data data = new DatA (obj);
		Data cur = i;
				while (cur!= null) {if (Cur.obj.equals (target)) {data.right = Cur.right;
				Data.left = cur;
				if (cur = last) is last = data;
				else Cur.right.left = data;
				Cur.right = data;
			return true;
		} cur = cur.right;
	return false;
		The public Object Deletefirst () throws exception{if (A/= null) throw new Exception ("empty!");
		Data temp = i;
			if (first.right = = null) {a = null;
		last = null;
			}else{first.right.left = null;
		i = first.right;
	return to temp;
		The public Object Deletelast () throws exception{if (A/= null) throw new Exception ("empty!");
		Data temp = last;
			if (first.right = = null) {a = null;
		last = null;
			}else{last.left.right = null;
		last = Last.left;
	return to temp;
		The public object Delete (Object obj) throws exception{if (A/= null) throw new Exception ("empty!");
		Data cur = i;
		while (cur!= null) {if (cur.obj.equals (obj)) {		if (cur = last) last = Cur.left;
				else Cur.right.left = Cur.left;
				if (cur = = cur.right;
				else cur.left.right = cur.right;
			return obj;
		} cur = cur.right;
	return null;
		public void display () {System.out.print ("I-> Last:");
		Data data = A;
			while (data!= null) {System.out.print (data.obj.toString () + "->");
		data = Data.right;
	} System.out.print ("\ n");
		public static void Main (string[] args) throws exception{doublylinklist dll = new doublylinklist ();
		Dll.insertfirst (1);
		Dll.insertlast (3);
		Dll.insertafter (1, 2);
		Dll.insertafter (3, 4);
		Dll.insertafter (4, 5);
		Dll.display ();
		Dll.deletefirst ();
		Dll.display ();
		Dll.deletelast ();
		Dll.display ();
		Dll.delete (3);
		Dll.display ();
		Dll.delete (2);
		Dll.display ();
		Dll.delete (4);
	Dll.display ();
 }
}
-> last:1-> 2-> 3-> 4-> 5-> a 
-> last:2-> 3-> 4-> 5-> 
firs T-> last:2-> 3-> 4-> a-> last:2 
-> 4-> a-> last:4-> 
 


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.