Doubly linked list:
is a two-way pointer, two-way chain.
The structure of the link node:
┌────┬────┬────────┐
│data│next│previous│
└────┴────┴────────┘
The doubly linked list does not have to be a double-ended list (which holds a reference to the last link), and the double-ended list is bidirectional when inserted.
There are two chains: one from beginning to end, one from the tail, and the other is two-way.
/** * Doubly linked list * * @author stone */public class Doublylinkedlist<t> {private link<t> head;//first node private link<t& Gt rear;//tail pointer public doublylinkedlist () {}public T peekhead () {if (head! = null) {return head.data;} return null;} public Boolean isEmpty () {return head = = null;} public void Insertfirst (T data) {//inserted into the chain header link<t> NewLink = new link<t> (data); if (IsEmpty ()) {//Is empty, the 1th time the new node is inserted For the tail node rear = NewLink;} else {head.previous = NewLink;//The upper node of the old Head node equals the new node}newlink.next = head; The lower node of the new node has the old head node head = NewLink; After assignment, the lower node of the head node is the old Head node, the upper node null}public void Insertlast (T data) {//At the end of the chain is inserted link<t> NewLink = new link<t> (data); if ( IsEmpty ()) {head = NewLink;} else {rear.next = NewLink;} newlink.previous = Rear;rear = NewLink; After assignment, the upper node of the tail node is the old tail node, the lower node null}public T deletehead () {//delete the link header if (isEmpty ()) return null; link<t> temp = Head;head = Head.next; Change the first node, for the next node if (head! = null) {head.previous = null;} else {rear = null;} return temp.data;} Public T Deleterear () {//delete chain tails if (isemptY ()) return null; link<t> temp = Rear;rear = rear.previous; Change the tail node, for the previous node if (rear! = null) {Rear.next = null;} else {head = null;} return temp.data;} Public t find (T T) {//From 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.P REVIOUS;IF (rear! = null) {Rear.next = null;}} else {//middle non-ends of nodes, to remove currentcurrent.next.previous = Current.previous;current.previous.next = Current.next;} return current.data;} public boolean InsertAfter (t key, T data) {//inserted after key, key does not exist 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 head, consider the back pointer of the head, the front pointer of the pointer after the original, the front and back pointers of the new link node 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;} El se {newlink.next = current.next;current.next.previous = Newlink;//current.next = Newlink;//newlink.previous = current;} *///The above judgment is written as follows: 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.out.println from the Beginning ("List (first-->last):"); Link<t> current = Head;while (current! = null) {Current.displaylink (); current = Current.next;}} public void Displaylist4rear () {//starts from the tail to traverse System.out.println ("List (Last-->first):"); Link<t> CUrrent = Rear;while (current! = null) {Current.displaylink (); current = Current.previous;}} Class Link<t> {//Link node T data;//data field link<t> next;//successor pointer, node chain field link<t> previous;//precursor pointer, node link domain link (t data) { This.data = data;} void DisplayLink () {System.out.println ("The data is" + data.tostring ());}} public static void Main (string[] args) {doublylinkedlist<integer> list = new doublylinkedlist<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 ("----in the specified kEY insert----"); List.insertafter (2, 8); List.insertafter (2, 9); List.insertafter (9, ten); List.displaylist4head ();}}
Print
List (first-->last): The data is 4the data are 2the data is 1the data are 3the data is 5deletehead:4list (first-->last) : The data is 2the data are 1the data is 3the data are 5deleterear:5list (Last-->first): The data is 3the data is 1the data is 2find:nullfind:3delete find:nulldelete find:1list (first-->last): The data is 2the data is 3----after the specified key is inserted----List (f Irst-->last): The data is 2the data are 9the data is 10the data are 8the data is 3
Java implements a doubly linked list