Java Foundation--a doubly-linked list of linked list structures

Source: Internet
Author: User

double-ended linked list: A double-ended list is very similar to a traditional linked list. Just a new attribute is added-that is, the reference to the last link node as shown: Because there is a direct reference to the last link. So the double-ended list is more convenient than the traditional linked list in some ways. For example, insert a link node at the end. The double-ended list can be operated directly but the traditional list is only able to loop through the next node to find the last link. So the double-ended list is suitable for manufacturing queues The following double-ended linked list class. There are several important methods. insertfirst (Insert first link node) This method is basically the same as the single-linked list in the previous post. The only difference is that there is a last-referenced operation. Normal because last is a reference to the tail link node, So inserting the first link node is irrelevant to him. However, when the link node is empty (IsEmpty ()), this appends the link node as both the first link and the tail link. So you need to point to it.

Public void insertfirst ( Double dd)  {    link newlink = new  link (DD)     if (IsEmpty ()) {        last  = newlink;     }      newlink.next = first;      first = newlink;     

insertlast (insert tail link) insertion of the tail chain node is also consistent with the general understanding, so do not repeat. The only thing to note is that the link node is empty (IsEmpty ()). You need to point first to the link node.

PublicvoidInsertlast (double dd)  {    link  newlink =  new link (dd);     if (IsEmpty ())  {         first = newlink;      }else {         last.next = newlink;     }     last =  newlink;}

Take a look at the insertion of the trailing link node to the reference point: deletefirst (delete header link) This should be noted, if only one link node is left. Then the last drop should point to null.

public void Deletefirst () {First = first.next;     if(first.next = = null) {Last = null; }     }

The final code is as follows:

Firstlastlink PackageCOM.DBSTRUCTOR.OOP3;
//Link Node class ClassLink {PublicDoubleDData;PublicLink Next;PublicLink (DoubleDD) {DData=dd }PublicvoidDisplayLink () {System.out.print (dData+""); } }//Double-ended linked list class Classfirstlastlist {PublicLink first;PublicLink last;PublicFirstlastlist () {First=Null; Last=Null; }PublicBooleanIsEmpty () {Return(First==Null); }//Table header Insertion PublicvoidInsertfirst (DoubleDD) {Link NewLink=NewLink (DD);If(IsEmpty ()) {Last=NewLink; } newlink.next=First First=NewLink; }//Footer Insertion PublicvoidInsertlast (DoubleDD) {Link NewLink=NewLink (DD);If(IsEmpty ()) {First=NewLink; }Else{Last.next=NewLink; } Last=NewLink; }//Delete a table header PublicvoidDeletefirst () {First=First.next;If(First.next==Null) {Last=Null; }         }PublicvoidDisplaylist () {System.out.print ("List (First--->last)"); Link Current=FirstWhile(Current!=Null) {Current.displaylink (); Current=Current.next;     } System.out.println (); } }PublicClassFirstlastapp {PublicStaticvoidMain (string[] args) {firstlastlist thelist=NewFirstlastlist ();//Inserting linked header data Thelist.insertfirst (22); Thelist.insertfirst (44); Thelist.insertfirst (66);//Inserting linked footer data Thelist.insertlast (113355//   Delete header data          thelist.deletefirst ();         thelist.deletefirst ();                   Thelist.displaylist ();     }
}

The result of the code operation is:

List (First--->last)66.044.022.011.0 33.0 55 0  list  (First--->last) 22. 0 11.0 33 0 55. 0 

the efficiency of a linked list here, by the way, is the advantage of efficiency compared to the list of links and arrays. The insertion and deletion of the table header is fast, because only a change of the reference is required to spend O (1) of the time. On average, finding, deleting, and inserting data after a specified node requires searching for half of the chain nodes. An O (N) comparison and an array are required. However, since the list is deleted, it is not necessary to move the elements like arrays. So efficiency is better than arrays. Another thing is that the memory of the linked list can be expanded at any time. The memory of the array is fixed at the beginning. This results in a significant decrease in the efficiency and usability of the array.

Java Foundation--a doubly-linked list of linked list structures

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.