Java implements a doubly linked list

Source: Internet
Author: User

Java implementation loop linked list: http://www.cnblogs.com/lixiaolun/p/4643911.html

In a single-linked list, the time to query the next element is O (1). The time to query the previous element is O (n).

To overcome this shortcoming, we began to learn the doubly linked list.

Code for the doubly linked list class:

Package Doublelinkedlist;public class Doublelinkedlist {class element{private Element prior=null;public Object value= Null;private Element Next=null;} Private Element Header = null;//Header node/** * Initialize linked list * */void initlist () {header = new Element (); header.prior=header;header.value =null;header.next=header;} /** * Insert Element O * */void insertlist (Object o,int i) {if (i<=0| |) to the first position in the linked list I>size ()) {System.out.println ("the insertion position is not legal! The linked list length is: "+size ());} Else{element e = new Element (); E.value=o;if (Header.prior==header)//Insert element {E.prior=header;e.next=header for the first time; Header.next=e;header.prior=e;} else if (i==size ())//At the end of the insert {System.out.println ("Insert at the tail of the list"); e.next=header;e.prior=header.prior;header.prior.next=e; Header.prior=e;} Else{element temp = Header;int Count=0;while (temp.next!=header) {count++;if (count = = i) {E.prior=temp;e.next=temp.next ; temp.next.prior=e;temp.next=e;} Temp=temp.next;}}}} /** * Delete an element in the linked list * */void deletelist (int i) {if (i<=0| | I>size ()) {System.out.println ("the insertion position is not legal! The linked list length is: "+size ());} Else{int count=0; Element temp = HeAder;while (Temp.next!=header) {temp=temp.next;count++;if (I==count) {//delete element I temp.next.prior=temp.prior; Temp.prior.next=temp.next;}}}} /** * Print List * */void print () {System.out.print ("Print two-way loop linked list:"); Element temp = header;while (temp.next!=header) {System.out.print (temp.next.value+ "\ t"); temp=temp.next;} System.out.println ();} /** * Get the size of the list * */int size () {int count=1; Element temp = header;while (temp.next!=header) {count++;temp=temp.next;} return count;}}

Test classes for doubly linked lists

Package Doublelinkedlist;public class Doublelinkedlistmain {public static void main (string[] args) {doublelinkedlist Dllist = new Doublelinkedlist (),//With head node Dllist.initlist ();d llist.insertlist (1, 1);d llist.insertlist (2, 2); Dllist.insertlist (3, 1);d llist.insertlist (4, 1);d llist.insertlist (5, 1);d llist.insertlist (6, 6);d llist.print (); Dllist.deletelist (3);d llist.print ();}}

  

Java implements a doubly linked list

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.