Java implements two-way linked list

Source: Internet
Author: User

Java implements two-way linked list

Two-way linked list:

There is a bidirectional pointer, that is, a bidirectional Chain Domain.

Link node structure:

┌ ── ─ ┬ ── ─ ┐

│ Data │ next │ previous │

└ ── ─ ┴ ── ─ ┘

A two-way linked list does not need to be a double-ended linked list (holding a reference to the last link node ).

There are two chains: one chain is from start to end, and the other chain is from the end to the end, and the deletion time is also bidirectional.

/*** Two-way linked list ** @ author stone */public class doublyshortlist
 
  
{Private Link
  
   
Head; // The first node private Link
   
    
Rear; // tail pointer public doublydomainlist () {} public T peekHead () {if (head! = Null) {return head. data;} return null;} public boolean isEmpty () {return head = null;} public void insertFirst (T data) {// insert to Link
    
     
NewLink = new Link
     
      
(Data); if (isEmpty () {// if it is null, the new node inserted 1st times is the end node rear = newLink;} else {head. previous = newLink; // The previous node of the old header node is equal to the new node} newLink. next = head; // The old header node head of the new node is newLink; // After the value is assigned, the next node of the new node is the old header node, last node null} public void insertLast (T data) {// insert Link at the end of the chain
      
        NewLink = new Link
       
         (Data); if (isEmpty () {head = newLink;} else {rear. next = newLink;} newLink. previous = rear; rear = newLink; // After the value is assigned, the upper node of the end node is the old end node, and the lower node is null} public T deleteHead () {// Delete the chain header if (isEmpty () return null; Link
        
          Temp = head; head = head. next; // change the first node to the next node if (head! = Null) {head. previous = null;} else {rear = null;} return temp. data;} public T deleteRear () {// delete the end of the chain if (isEmpty () return null; Link
         
           Temp = rear; rear = rear. previous; // change the end node to the previous node if (rear! = Null) {rear. next = null;} else {head = null;} return temp. data;} public T find (T t) {// findif (isEmpty () {return null;} Link
          
            Find = head; while (find! = Null) {if (! Find. data. equals (t) {find = find. next;} else {break;} if (find = null) {return null;} return find. data;} public T delete (T t) {if (isEmpty () {return null;} Link
           
             Current = head; while (! Current. data. equals (t) {current = current. next; if (current = null) {return null ;}} if (current = head) {head = head. next; if (head! = Null) {head. previous = null ;}} else if (current = rear) {rear = rear. previous; if (rear! = Null) {rear. next = null ;}} the non-two-End Node In The Middle Of else {//, to remove currentcurrent. next. previous = current. previous; current. previous. next = current. next;} return current. data;} public boolean insertAfter (T key, T data) {// after the key is inserted, the key does not have return falseif (isEmpty () {return false;} Link
            
              Current = head; while (! Current. data. equals (key) {current = current. next; if (current = null) {return false ;}} Link
             
               NewLink = new Link
              
                (Data);/* if (current = head) {// if it is a header, consider the back pointer of the header, the front pointer of the back pointer of the original header, newLink. next = current. next; current. next. previous = newLink; // newLink. previous = current; // current. next = newLink;} else if (current = rear) {// newLink. next = null; rear = newLink; // current. next = newLink; // newLink. previous = current;} else {newLink. next = current. next; current. next. previous = newLink; // current. next = newLink; // newLink. previous = current;} * // The preceding judgment is abbreviated as: if (current = rear) {rear = newLink;} else {newLink. next = current. next; current. next. previous = newLink;} current. next = newLink; newLink. previous = current; return true;} public void displayList4Head () {// traverse System from the beginning. out. println ("List (first --> last):"); Link
               
                 Current = head; while (current! = Null) {current. displayLink (); current = current. next ;}} public void displayList4Rear () {// traverse System from the end. out. println ("List (last --> first):"); Link
                
                  Current = rear; while (current! = Null) {current. displayLink (); current = current. previous ;}} class Link
                 
                   {// Link node T data; // Link of the data domain
                  
                    Next; // subsequent pointer, Link of the node Chain Domain
                   
                     Previous; // The front pointer, Link (T data) of the node chain domain {this. data = data;} void displayLink () {System. out. println ("the data is" + data. toString () ;}} public static void main (String [] args) {doubly#list
                    
                      List = new doublyasklist
                     
                       (); List. insertLast (1); list. insertFirst (2); list. insertLast (3); list. insertFirst (4); list. insertLast (5); list. displayList4Head (); Integer deleteHead = list. deleteHead (); System. out. println ("deleteHead:" + deleteHead); list. displayList4Head (); Integer deleteRear = list. deleteRear (); System. out. println ("deleteRear:" + deleteRear); list. displayList4Rear (); System. out. println ("find:" + list. find (6); System. out. println ("find:" + list. find (3); System. out. println ("delete find:" + list. delete (6); System. out. println ("delete find:" + list. delete (1); list. displayList4Head (); System. out. println ("---- insert ----" after specifying the key); list. insertAfter (2, 8); list. insertAfter (2, 9); list. insertAfter (9, 10); list. displayList4Head ();}}
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

Print

List (first --> last): the data is 4the data is 2the data is 1the data is 3the data is 5 deleteHead: 4 List (first --> last ): the data is 2the data is 1the data is 3the data is 5 deleteRear: 5 List (last --> first): the data is 3the data is 1the data is 2 find: nullfind: 3 delete find: nulldelete find: 1 List (first --> last): the data is 2the data is 3 ---- insert ---- List (first --> last) after the specified key ): the data is 2the data is 9the data is 10the data is 8the data is 3


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.