1. Node
The storage unit used to store a single data in the linked list.
A linked list must be composed of at least two parts: the data domain and the pointer domain. Generally, nodes are defined:
Struct Node
{
ELEM data; // The ELEM type refers to the basic data type.
Struct node * next;
}
Typedef struct node elemsn;
The above two steps are equivalent:
Typedef struct Node
{
ELEM data;
Struct node * next;
} Elemsn;
2. Use the pointer Variable P to represent node members.
* P. Data // error. The correct value is (* P). Data.
P-> data // correct
P-> next // correct
Note: (.), (->), and ([]) operators have the highest priority in C ).
3. Precursor node; the previous node of a node.
Successor node: The last node of a node.
Features of one-way linked list:
(1) There is only one node without a precursor, that is, the header node. The header node points to it through the head pointer.
(2) There is only one node with no successor, that is, the end node. The value of the next field at the end node is null.
(3) All nodes except the head and end nodes have only one precursor and only one successor.
4. Example
Example 1: the most basic implementation method for creating a one-way linked list.
# Include <stdio. h> # include <malloc. h> # definenull0typedef struct node {int data; struct node * Next; // elemsn} elemsn cannot be used; int main () {elemsn * head, * P; int I, MS; /* ms is used to store the number of nodes */int x; printf ("Please input node number:"); scanf ("% d", & MS ); head = P = (elemsn *) malloc (sizeof (elemsn); printf ("Please input data:"); scanf ("% d", & X ); head-> DATA = x; head-> next = NULL;/* create the first node because it is the first node, it is somewhat different from the subsequent node, so it is written separately */for (I = 0; I <MS-1; I ++)/* create the following MS-1 nodes */{P-> next = (elemsn *) malloc (sizeof (elemsn )); printf ("Please input data:"); scanf ("% d", & X); P-> next-> DATA = x; p-> next = NULL; P = p-> next;} For (P = head; P! = NULL; P = p-> next) {printf ("% d", p-> data);} printf ("\ n ");}