A circular linked list is a chain table that is connected at the beginning and end.
1. Circular linked list
(1) single-cycle linked list-in a single-chain table, change the pointer field null of the terminal node to the header node or the Start Node.
(2) Cyclic linked list of multiple links-link the nodes in the table to multiple loops.
2. Single-cycle linked list of the lead Node
Note:
The condition for determining an empty linked list is head = head-> next;
3. Single-loop linked list with only the tail pointer
The single-loop linked list represented by the end pointer 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.
Note:
The condition for determining an empty linked list is rear = rear-> next;
4. Cyclic linked list features
The cyclic linked list feature is that you do not need to increase the storage capacity, but only slightly change the table link method, which makes table processing more convenient and flexible.
[Example] Two Linear tables (A1, A2 ,..., An) and (B1, B2 ,..., BM) is connected to a linear table (A1 ,..., An, B1 ,... BM.
Analysis:If you perform this link operation on a single-cycle table indicated by a single-chain table or a header pointer, You need to traverse the first linked list, locate the node an, and then link node B1 to the end of, the execution time is O (n ). If it is implemented on the single-loop linked list indicated by the tail pointer, you only need to modify the pointer and do not need to traverse it. The execution time is O (1 ).
The algorithm is as follows:
Linklist connect (linklist A, linklist B)
{// Assume that A and B are the tail pointers of non-empty cyclic linked lists.
Linklist P = A-> next; // ① Save the position of the header node of Table
A-> next = B-> next; // link the Start Node of Table B to the end of Table.
Free (B-> next); // ③ release the header node of Table B
B-> next = P; // ④
Return B; // return the tail pointer of the new cyclic linked list.
}
Note:
① The Circular linked list does not contain a null pointer. When a traversal operation is involved, the termination condition does not determine whether P or p-> next is null as the non-cyclic linked list, but whether they are equal to a specified pointer, such as the header pointer or tail pointer.
② In a single-chain table, starting from a known node, only the node and its subsequent nodes can be accessed, and other nodes before the node cannot be found. In a single-cycle linked list, all nodes in the table can be accessed from any node. This advantage makes some operations easy to implement on a single-cycle linked list.