Java single chain table, two-end linked list, ordered list realization __java

Source: Internet
Author: User

single linked list :

Insertfirst: Inserts a new link point in the table header, the time complexity is O (1)

Deletefirst: Delete the link point of the header, the time complexity of O (1)

With these two methods, you can use a single linked list to implement a stack, see http://blog.csdn.net/a19881029/article/details/22579759

Find: Look for links that contain the specified keywords, and because you need to traverse the lookup, you need to find N/2 times, that is, O (N)

Remove: Delete the link point containing the specified keyword, and because you need to traverse the lookup, you need to find the N/2 times, that is, O (N)

public class LinkedList {private class data{private Object obj;
		
		Private Data next = null;
		Data (Object obj) {this.obj = obj;
	
	} private Data a = null;
		public void Insertfirst (Object obj) {Data data = new data (obj);
		Data.next = A;
	i = data;
		The public Object Deletefirst () throws exception{if (A/= null) throw new Exception ("empty!");
		Data temp = i;
		i = First.next;
	return temp.obj; 
		Public Object find (Object obj) throws exception{if (A/= null) throw new Exception ("LinkedList is empty!");
		Data cur = i;
			while (cur!= null) {if (cur.obj.equals (obj)) {return cur.obj;
		} cur = cur.next;
	return null; 
		public void Remove (Object obj) throws exception{if (A/= null) throw new Exception ("LinkedList is empty!");
		if (first.obj.equals (obj)) {i = First.next;
			}else{Data pre = i;
			Data cur = first.next; while (cur!= null) {if (cur.obj.equals (obj)) {pre.neXT = Cur.next;
				Pre = cur;
			cur = cur.next;
	}} public Boolean IsEmpty () {return (a = null);
		public void display () {if (A/= null) System.out.println ("Empty");
		Data cur = i;
			while (cur!= null) {System.out.print (cur.obj.toString () + "->");
		cur = cur.next;
	} System.out.print ("\ n");
		public static void Main (string[] args) throws Exception {LinkedList ll = new LinkedList ();
		Ll.insertfirst (4);
		Ll.insertfirst (3);
		Ll.insertfirst (2);
		Ll.insertfirst (1);
		Ll.display ();
		Ll.deletefirst ();
		Ll.display ();
		Ll.remove (3);
		Ll.display ();
		System.out.println (Ll.find (1));
	System.out.println (Ll.find (4));
 }
}
1-> 2-> 3-> 4-> 
2-> 3-> 4-> 
2-> 4-> 
null
4

double-end linked list (not doubly linked list):

The difference between a one-way linked list and a reference to the last link is saved

Insertfirst: Inserts a new link point in the table header, time complexity O (1)

Insertlast: Inserts a new link point at the end of the table, time complexity O (1)

Deletefirst: Delete the header of the chain contact, time complexity O (1)

Deletelast:: Delete the link point at the end of the table, because only the chain point at the end of the table is saved, instead of saving the first link point of the end of the table (this shows the advantage of a two-way list), you need to traverse to find the first chain contact point of the tail chain of the table when you delete the tail chain contact, look for n-1 times, i.e. O (N)

With these methods you can use a two-terminal list to implement a queue, http://blog.csdn.net/a19881029/article/details/22654121

public class Firstlastlist {private class data{private Object obj;
		
		Private Data next = 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) last = data;
		Data.next = A;
	i = data;
		public void Insertlast (Object obj) {Data data = new data (obj);
		if (A/= null) {a = data;

		}else{last.next = data;
	last = data; Public Object Deletefirst () throws exception{   if (A/= null)     throw new exceptio
N ("empty");
   Data temp = i;
   if (first.next = null)     last = null;
   i = First.next;
   return temp.obj;
		 } public void Deletelast () throws exception{if (A/= null) throw new Exception ("Empty");
			if (First.next = = null) {a = null;
		last = null; }else{Data Temp = A;
					while (Temp.next!= null) {if (Temp.next = last) {last = temp;
					Last.next = null;
				Break
			temp = Temp.next;
		}} public void display () {if (A/= null) System.out.println ("Empty");
		Data cur = i;
			while (cur!= null) {System.out.print (cur.obj.toString () + "->");
		cur = cur.next;
	} System.out.print ("\ n");
		public static void Main (string[] args) throws Exception {firstlastlist fll = new Firstlastlist ();
		Fll.insertfirst (2);
		Fll.insertfirst (1);
		Fll.display ();
		Fll.insertlast (3);
		Fll.display ();
		Fll.deletefirst ();
		Fll.display ();
		Fll.deletelast ();
	Fll.display (); }
}
1-> 2-> 
1-> 2-> 3-> 
2-> 3-> 

ordered list: data in a linked list is arranged from small to large

public class SortedList {private class data{private Object obj;
		
		Private Data next = null;
		Data (Object obj) {this.obj = obj;
	
	} private Data a = null;
		public void Insert (Object obj) {Data data = new data (obj);
		Data pre = NULL;
		Data cur = i; while (cur!= null && (Integer.valueof (data.obj.toString ()). Intvalue () > Integer.valueof (cur.obj.toString (
			). Intvalue ()) {pre = cur;
		cur = cur.next;
		} if (pre = = null)-A/data;
		else Pre.next = data;
	Data.next = cur;
		The public Object Deletefirst () throws exception{if (A/= null) throw new Exception ("empty!");
		Data temp = i;
		i = First.next;
	return temp.obj;
		public void display () {if (A/= null) System.out.println ("Empty");
		System.out.print ("-> Last:");
		Data cur = i;
			while (cur!= null) {System.out.print (cur.obj.toString () + "->");
		cur = cur.next;
	} System.out.print ("\ n"); } public static VoiD main (string[] args) throws exception{SortedList sl = new SortedList ();
		Sl.insert (80);
		Sl.insert (2);
		Sl.insert (100);
		Sl.display ();
		System.out.println (Sl.deletefirst ());
		Sl.insert (33);
		Sl.display ();
		Sl.insert (99);
	Sl.display (); }
}
-> last:2->->-> 
2 a
-> last:33->--> 

Table insertion and deletion average need to compare N/2 times, that is, O (N), but get the smallest data items only O (1), because it is always in the header, for the application of frequent operation of the smallest data items, you can consider the use of sequential linked list implementation, such as: Priority queue

Compared with arrays, the advantage of a linked list is that the length is unrestricted, and when inserting and deleting operations, you do not need to move the data items, so even though some operations have the same time complexity as the array, the actual efficiency is much higher than the array.

The disadvantage lies in random access and cannot find specific data items directly by subscript as arrays do

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.