Definition:
Single-cycle linked list-in a single-chain table, change the NULL pointer field of the terminal node to the header node or the Start Node.
Leading Node
The condition for determining the empty linked list is head = head. next;
Tail Node
The single-cycle linked list represented by the last node rear is O (1) for the Start Node a1 and End Node an lookup time ). Table operations are often performed at the beginning and end of a table. Therefore, in practice, the tail pointer is used to represent a single-loop linked list. The single-cycle linked list with the tail pointer is visible.
650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15540631D-0.jpg "class =" editorImg log-set-param "alt =" cyclic linked list "title =" cyclic linked list "height =" 63 "width =" 200 "/>
Note: The condition for determining an empty linked list is rear = rear. next;
The operation of the cyclic linked list is basically the same as that of the single-linked list. The differences are as follows:
1. When creating a circular linked list, you must point the pointer of the last node to the header node instead of being NULL as a single-linked table. In this case, a new node is inserted after the last node.
2. When determining whether the end of the table is reached, it is to determine whether the value of the node chain domain is the header node. When the Chain Domain value is equal to the header pointer, it indicates that it has reached the end of the table. Instead of determining whether the Chain Domain value is NULL like a single-chain table.
Implementation of a single cyclic linked list:
Package com. ds. link; public class CircularLinkedList <T> {private Node <T> head; private int length; // the length of the list private class Node <T> {private Node <T> next; private T data; public Node (T data) {this. data = data; next = null;} public Node (T data, Node <T> nextNode) {this. data = data; next = nextNode;} public void setData (T data) {this. data = data;} public void setNext (Node <T> next) {This. next = next;} public T getData () {return data;} public Node <T> getNext () {return next ;}} public CircularLinkedList () {head = new Node <T> (null, head); length = 0;} public void insertAtPrior (T item) {// Add a Node <T> node = new Node <T> (item, null) after the header node; // encpsule the item to an node Node. setNext (head. getNext (); // node. next = head. next head. setNext (node); // head. next = node length ++ ;} Public void add (T item) {// add a Node on the last Node <T> tmp = head; if (isEmpty ()) {// if the list is null Node <T> node = new Node <T> (item, head );//.. if next = null? Head. setNext (node);} else {Node <T> node = new Node <T> (item, head); // find the end node of the list while (head! = Tmp. getNext () {tmp = tmp. getNext ();} tmp. setNext (node) ;}length ++;} public T get (int index) {// get the data of the node at the index // first judge the correctness of the index if (index> length | index <0) {throw new RuntimeException ("the index value is incorrect: "+ index);} else if (isEmpty () {return null;} else {Node <T> tmp = head; int I = 1; while (head! = Tmp. getNext () & I <= index) {tmp = tmp. getNext (); I ++;} T e = tmp. getData (); return e ;}} public void insert (int index, T item) {// Add the Node <T> node = new Node <T> (item, null) after the index; Node <T> tmp = head; int I = 1; if (index> length | index <0) {System. out. println ("the index is out of bounds");} else if (0 = length & 1 = index) {node. setNext (head); head. setNext (node); length ++;} else {// Find the node index while (head! = Tmp. getNext () & I <= index) {tmp = tmp. getNext (); I ++;} node. setNext (tmp. getNext (); tmp. setNext (node); length ++ ;}} public void removeFromFront () {// Node of the first node after the header Node is deleted <T> tmp = head; if (length <1) {System. out. println ("The list is null and you can not delete any node! ");} Else if (1 = length) {head. setNext (head); length --;} else {head. setNext (tmp. getNext (). getNext (); length -- ;}} public void remove (int index) {// Delete the node at the index if (length <1 | index> length) {System. out. println ("index is out of bounds");} else if (1 = length & 1 = index) {head. setNext (head); length --;} else {Node <T> tmp = head; int I = 1; // get the node before index while (head! = Tmp. getNext () & I <index) {tmp = tmp. getNext (); I ++;} tmp. setNext (tmp. getNext (). getNext (); length -- ;}} public void removeFromLast () {// Delete the last node if (length <1) {// if the list is null System. out. println ("The list is null and you can not delete");} else if (1 = length) {head. setNext (head); length --;} else {Node <T> tmp1 = head; Node <T> tmp2 = head. getNext (); // set tmp2-tmp1 = 1 while (Head! = Tmp2.getNext () {tmp2 = tmp2.getNext (); tmp1 = tmp1.getNext ();} tmp1.setNext (head); length -- ;}} public int getLength () {return length ;} public boolean isEmpty () {return length = 0;} public void display () {if (length <1) {System. out. println ("The list is null");} else {Node <T> tmp = head; while (head! = Tmp. getNext () {tmp = tmp. getNext (); System. out. print (tmp. getData () + "") ;}}// test the list public static void main (String [] args) {CircularLinkedList <Integer> l = new CircularLinkedList <Integer> (); System. out. println (l. isEmpty (); l. add (1); l. add (2); l. insertAtPrior (3); l. insert (2, 4); l. add (5); System. out. println ("the list is:"); l. display (); System. out. println (); System. out. println ("the length is:" + l. getLength (); l. removeFromFront (); l. removeFromLast (); l. display (); // System. out. println (l. get (3 ));}}
Result output:
truethe list is :3 1 4 2 5the length is :51 4 2
If you have any questions, feel free to contact us!
This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1288752