Figure 1 shows the logical states of a linear table (Zhao, Qian, sun, Li, Zhou, Wu, Zheng, and Wang.Header pointer Indicates the storage location of the first node (the storage image of the first data element) in the linked list. At the same time, because the last data element is not directly followed, the pointer to the last node in the linear linked list is "null" (null ).
Figure 1 logical state of a Linear Linked List
As shown in the preceding description, a single-chain table can be uniquely identified by a header pointer, which can be described by a "structure Pointer" in C language.
- // ----- Storage structure of a single-chain table of a linear table -----
- Typedef StructLnode {
- Elemtype data;
- StructLnode * next;
- } Lnode, * linklist;
-
Sometimes a node is attached to the first node of a single-chain table.Header Node . The data field of the header node can store additional information such as the length of a linear table, without any information, the pointer field of the header node stores the pointer to the first node (that is, the storage location of the first element node ). 2 (a). At this time, the header pointer of the single-chain table points to the header node. If the linear table is empty, the pointer field of the header node is "null", as shown in 2 (B.
Figure 2 single-chain table of the lead Node(A) Non-empty table; (B) empty table
Cyclic linked list Is another form of chained storage structure. It points the pointer field of the last node in the table to the header node, and the entire linked list forms a ring. As a result, other nodes in the table can be found from any node in the table, as shown in 3, which is a single-chain circular linked list..
Figure 3 single-chain cyclic table (a) non-empty table; (B) empty table
The operation of the circular linked list is basically the same as that of the Linear Linked List. The difference is thatAlgorithmThe cyclic condition in is notP or p-> nextWhether it is null, but whether it is equal to the header pointer. However, if you set up the tail pointer in the circular linked list without the header pointer (4 (), some operations can be simplified. For example, to merge two linear tables into one table, you only need to link the end table of one table with the header table of another table. When a linear table uses a circular linked list of 2.4 (a) as the storage structure, this operation only needs to change the two pointer values. The operation time isO(1 ). The merged table 4 (B) is shown in.
Figure 4 set only the circular linked list of the tail pointer (a) two linked lists; (B) the merged table
the nodes in the chain storage structure discussed above only have one pointer field indicating direct successor. Therefore, from a node, you can only search for other nodes along the pointer. To query the direct frontend of a node, you need to start from the header pointer. In other words, in a single-chain table, the nextelem execution time is O (1), and the priorelem execution time is O (n ). two-way linked list can be used to overcome the unidirectional disadvantage of a single-chain table. As the name suggests, there are two pointer fields in the node of the two-way linked list, one pointing to direct successor, and the other pointing to direct frontend. The C language can be described as follows:
- // ----- Two-way linked list storage structure of a linear table -----
- Typedef StructDulnode {
- Elemtype data;
- StructDulnode * Prior;
- StructDulnode * next;
- } Dulnode, * dulinklist;
Similar to a single-chain circular table, a two-way linked list can also have a circular table, as shown in 5 (c). The linked list contains two loops, as shown in Figure 5 (B) this is an empty table with only one header node. In a two-way linked list, if D is a pointer to a node in the table (that is, D is a dulinklist variable ),
D-> next-> prior = D-> prior-> next = d
Figure 5 bidirectional linked list example (a) node structure; (B) Empty bidirectional cyclic linked list; (c) non-empty bidirectional cyclic linked list