Two-way (Cyclic) linked list is another form of chain storage structure of linear tables.
We have already talked about one-way linked list and circular linked list. Compared with the limitations that a one-way linked list can only traverse the entire linked list from the first node,The cyclic linked list allows you to traverse the entire linked list from any node..
However, no matter whether it is a one-way linked list or a circular linked list, you can only search for the next node (the next node) of a node ), but cannot find the previous node of the node (the front node ). In view of the above problemsTwo-way linked list. The two-way cyclic linked list contains all functions of the two-way linked list. Therefore, we only talk about two-way circular linked list.
Unlike a one-way linked list, the node Structure of a two-way linked list is shown in. A node is composed of three parts: DATA in the DATA field, llink in the left pointer field, and rlink in the right pointer field. The data field is used to store the node data information, the left pointer field is used to point to the front node, and the right pointer field is used to point to the next node. A two-way cyclic linked list has the following features: p-> llink-> rlink = p-> rlink-> llink = p.
The code used to represent two-way linked list nodes is as follows:
Public class Node {private Object data; private Node lLink, rLink; // constructor public Node () {this. data = null; this. lLink = null; this. rLink = null;} // The constructor loads public Node (Object data) {this. data = data; this. lLink = null; this. rLink = null;} public Node (Object data, Node lLink, Node rLink) {this. data = data; this. lLink = lLink; this. rLink = rLink;} // read node data public Object getData () {return data;} // write node data public void setData (Object data) {this. data = data;} // obtain the Node's precursor Node public Node getLeftLink () {return lLink;} // obtain the Node's successor Node public Node getRightLink () {return rLink ;} // set public void setLeftLink (Node lLink) {this. lLink = lLink;} // set public void setRightLink (Node rLink) {this. rLink = rLink ;}}
The code for the bidirectional cyclic linked list with header nodes is as follows. In the code example, only the operations for creating, inserting, deleting, and printing a two-way cyclic linked list are described.
Public class DoubleLink {/*** head Node of the bidirectional cyclic linked list with header nodes */private Node headNode = new Node (); /*** length of the linked list */private int length = 0;/*** create a two-way linked list with a header node ** @ param datas array, used to represent the data domains */public void createDoubleLink (Object [] datas) {Node p, r; r = headNode; for (int I = 0; I
Length) {return false;} else {Node p = new Node (item); if (pos = 0) {p. setLeftLink (headNode); p. setRightLink (headNode. getRightLink (); headNode. getRightLink (). setLeftLink (p); headNode. setRightLink (p);} else if (pos = length) {p. setLeftLink (headNode. getLeftLink (); p. setRightLink (headNode); headNode. getLeftLink (). setRightLink (p); headNode. setLeftLink (p);} else {int count = 1; Node r = headNode. getRightLink ( ); While (count <pos) {r = r. getRightLink (); count ++;} p. setLeftLink (r); p. setRightLink (r. getRightLink (); r. getRightLink (). setLeftLink (p); r. setRightLink (p) ;}return true ;}} /*** for the first time, the node whose data field is item appears in the two-way linked list of the leading node. ** @ param item: Delete the data field of the node. * @ return: the deletion is successful, and true is returned. Otherwise, returns false */public boolean deleteItem (Object item) {Node p = headNode. getRightLink (), r = headNode; while (p! = HeadNode) {if (p. getData () = item) {r. setRightLink (p. getRightLink (); p. getRightLink (). setLeftLink (r); return true;} else {r = p; p = p. getRightLink () ;}} return false ;}}