Big talk data structure (eight) Java program--the implementation of doubly linked list

Source: Internet
Author: User
Tags prev

Linear linked List-bidirectional linked list Two-way list definition:

Doubly linked list (double linked list): In each node of a single form, set a pointer field that points to the precursor node. Therefore, the nodes in the doubly linked list have two pointer fields, one pointing to the precursor and one pointing to the successor.

The storage structure of the doubly linked list

typedef struts dulnode{

Element data;

Struct dulnode *prior; precursor pointer

Struct Dulnode *next; successor pointer

}duldouble, *dullinklist;

Insert and delete doubly linked list inserts in a doubly linked list:

Assuming that the node is S, to insert the node into the node p and P->next, we need the following steps:

S.prev = p;

S.next = P.next;

P.next.prev = s;

P.next = s;

Summed up is: first to take the predecessor of S, and then the predecessor of the post-junction, and finally solve the successor of the predecessor node

Delete a doubly linked list:

To delete a P-node, you need only two steps:

P.next.prev = P.prev;

P.prev.next = P.next;

Free (p);

Summarize:

Relative to the single-linked list, the two-way linked list more than a pointer field, space to occupy a little more, but it has a good symmetry, making it easier for a node to operate before and after nodes, can improve the time performance of the algorithm, which is the sacrifice of space exchange.

Java programs implement doubly linked lists:

 Packagecom.aclie.dataStructe4.sqeList; Public classMydoublelinklist {Private intlength = 0;//Current Length    PrivateNode Head;//Head knot Point    PrivateNode tail;//Node node at the current point          Publicmydoublelinklist () {initlink (); }     Public voidInitlink () {head=NewNode (NULL); Tail=NewNode (NULL);  This. Head =tail; Length++; }        //Get the chain table length     Public intGetSize () {returnlength; }    //determine if the linked list is empty     Public BooleanGetempty () {returnGetSize () ==0; }    //find an element from the index start with the first valid value     PublicNode GetNode (intindex)        {Node p; if(Index < 0 | | index >length) {System.out.println ("Parameter Error"); }        if(Index < This. LENGTH/2) {p= This. Head;  for(inti=0; i<index; i++) {p=P.next; }        }Else{p= This. Tail;  for(inti= length; i>index;i--) {p=P.prev; }        }        returnp; }         PublicObject GetData (intindex) {        returngetnode (index). data; }        //inserting at the head node     Public BooleanAddHead (Object e) {//The precursor reference is null and the successor is nodeNode node =NewNode (E,NULL, This. Head); //change the predecessor of the head node         This. Head.prev =node;  This. Head =node; if(Tail = =NULL) {Tail= This. Head; } length++; return true; }    //Insert at Tail node     Public BooleanAddTail (Object e) {if( This. Head = =NULL){             This. Head =NewNode (E,NULL,NULL);  This. Tail = This. Head; }Else{node node=NewNode (E, This. Tail,NULL);  This. Tail.next =node;  This. Tail =node; } length++; return true; }    //inserts an element at a specified location     Public BooleanAddData (intindex,object ele) {        if(Index <0 | | index > This. Length) {System.out.println ("Parameter Error"); }        if( This. Head = =NULL){             This. AddTail (Ele);//using the tail interpolation method}Else{            if(Index = = 0) {addhead (ele);//using the head interpolation method}Else{Node P= This. GetNode (index);//the node to insert atNode n =P.next; Node Node=NewNode (Ele,p,n);//the node to insertN.prev =node; P.next=node; Length++; }        }                return true; }     Public voidRemovedata (intindex) {        if(Index < 0 | | index >length) {System.out.println ("Parameter Error"); }Else{Node del=NULL; if(Index = = 0) {del= This. Head;  This. Head = This. Head.next;  This. Head.prev =NULL; Length--; }Else if(Index = = (length-1) {Node P= This. GetNode (Index-1);//get the precursor node to delete the node.del = P.next;//the node to deleteP.next =Del.next; if(Del.next! =NULL) {Del.next.prev=p; } Del.next=NULL; Del.prev=NULL; Length--;  This. Tail.next =NULL;  This. Tail.prev =p;  This. Tail =p; }             Else{Node P= This. GetNode (Index-1);//to delete a node's predecessor nodedel = P.next;//the node to deleteP.next =Del.next; if(Del.next! =NULL) {Del.next.prev=p; } Del.prev=NULL; Del.next=NULL; Length--; }                      }    }    //Print all elements in a linked list     Public voidprint () {Node current= This. Head;  while(Current! =NULL) {System.out.println (current.data); Current=Current.next; }    }    //Reverse Print linked list     Public voidReverseprint () {Node current= This. Tail;  while(Current! =NULL) {System.out.println (current.data); Current=Current.prev; }    }     Public Static voidMain (String args[]) {mydoublelinklist linklist=Newmydoublelinklist (); Linklist.addhead ("AAAA");//System.out.println (Linklist.getdata (1));Linklist.addtail ("BBBB");//System.out.println (Linklist.getdata (3));Linklist.adddata (2, "eeee");//linklist.print ();Linklist.removedata (2);        Linklist.print (); System.out.println (".....");    Linklist.reverseprint (); }    }classnode{Node prev;//precursor in the pointer fieldNode Next;//subsequent in the pointer fieldObject data;//data fields     Publicnode (node current) {prev=Current ; Next=Current ; }    //successor of double-linked list and digital field     Publicnode (Object d, node P,node N) { This. data =D;  This. prev =p;  This. Next =N; }     PublicNode GetPrev () {returnprev; }     Public voidSetprev (Node prev) { This. prev =prev; }     PublicNode GetNext () {returnNext; }     Public voidSetnext (Node next) { This. Next =Next; }    }

Big talk data structure (eight) Java program--the implementation of 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.