Java implements two-way linked list and java implements

Source: Internet
Author: User

Java implements two-way linked list and java implements

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.

/*** Al linked list *** @ author stone */public class DoublyLinkedList <T> {private Link <T> head; // The first node private Link <T> rear; // tail pointer public doubly1_list () {} public T peekHead () {if (head! = Null) {return head. data;} return null;} public boolean isEmpty () {return head = null;} public void insertFirst (T data) {// insert to the Link header <T> newLink = new Link <T> (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 <T> newLink = n Ew Link <T> (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 <T> 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 <T> temp = rear; rear = rear. previous; // change the end node to the if (rear! = Null) {rear. next = null;} else {head = null;} return temp. data;} public T find (T t) {// findif (isEmpty () {return null;} Link <T> 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 <T> 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 <T> current = head; while (! Current. data. equals (key) {current = current. next; if (current = null) {return false ;}} Link <T> newLink = new Link <T> (data);/* if (current = head) {// if it is a header, consider the post-pointer of the header, the front pointer of the Post-pointer of the original header, and the front and back pointer newLink of the new link node. 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. previo Us = 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 <T> current = head; while (current! = Null) {current. displayLink (); current = current. next ;}} public void displayList4Rear () {// traverse System from the end. out. println ("List (last --> first):"); Link <T> current = rear; while (current! = Null) {current. displayLink (); current = current. previous ;}} class Link <T >{// Link node T data; // Link of the data domain <T> next; // next pointer, Link of the node chain domain <T> previous; // precursor pointer, Link (T data) {this. data = data;} void displayLink () {System. out. println ("the data is" + data. toString () ;}} public static void main (String [] args) {doublyshortlist <Integer> list = new doublyshortlist <Integer> (); 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



How to implement two-way linked list in Java

Custom Exception class: Java code public class MyException extends Exception {public MyException () {}; public MyException (String msg) {super (msg) ;}} linked list Node object: java code public class Node {public Node previou = null; // The Front Node pointer public Node next = null; // The rear Node pointer public Object value; // Node value Node (Object value) {this. value = value ;}} linked list object: Java code public class DoubleLinked {private Node head; // The linked list header DoubleLinked () {}/ *** determines whether there is another Node, if no node exists, it is the end Node * @ param node * @ return */public boolean hasNext (node) {if (Node. next = null) return false; return true;}/*** determines whether a previous node exists, if no node exists, it is the head Node * @ param node * @ return */public boolean hasPrev (node) {if (Node. previou = null) return false; return true;}/*** get the linked list Header element * @ return * @ throws MyException */public Node getHead () throws MyException {if (head = null) {throw new MyException ("Empty Linked List");} return head ;} /*** get the previous contact * @ param node * @ return */public Node getPrev (Node node) {return node. previou;}/*** get the next node * @ param Node * @ return */public Node getNext (node) {return node. next;}/*** retrieve Node Based on index * @ param index: Node index * @ return * @ throws MyException */public Node getNode (int index) throws MyException {Node curNode = null; Node next = null; Node node = null; if (head = null) {throw new MyException ("Empty Linked List ");} else {curNode = head; for (int I = 0; I <index; I ++) {if (curNode = null) {throw new MyException ("the element index you want to obtain is greater than the length of the Linked List");} else {node = curNode; if (hasNext (curNode )) {next = curNode ...... remaining full text>

What is the idea of implementing a bidirectional linked list in java?

The core is such a class: a node of a two-way linked list:
Class Node {
Private Node next;
Pirvate Node back;
/* Getter and setter */
}
 

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.