The C language implementation of the linked list: Implementation Details of a single-chain table, details of a single-chain table
I. Create a single-chain table
With the foundation of dynamic memory allocation, it is not difficult to implement a linked list.
A linked list is a data structure that stores linear table elements with any storage unit. The linked list can be further divided into single-chain tables, two-way linked lists, and cyclic linked lists. Let's talk about a single-chain table first. A single-chain table is a one-way arrangement of data points. A single-linked table node has two structural types:
1. Data domain: used to store its own data
2. A Chain Domain is also called a pointer domain. It is used to store the next node address or direct the pointer to it.
Example:
Typedef struct node
{
Char name [20];
Struct node * link;
} Stud;
In this way, the structure of a single-chain table is defined. char name [20] is an array of character types used to store names. The * link pointer is a pointer used to store its direct successor.
After the structure of the linked list is defined, as long as appropriate data is stored in the data domain when the program is running, if there are successor nodes, the chain domain is directed to its direct successor. If no, set to NULL.
The following describes a complete procedure for creating a single-chain table with a table header (if not described, the linked list with a table header.
# Include
# Include /* Header file containing the dynamic memory allocation function */
# Define N 10/* N indicates the number of people */
Typedef struct node
{
Char name [20];
Struct node * link;
} Stud;
Stud * creat (int n)/* create a single-chain table function. The parameter n is the number of people */
{
Stud * p, * h, * s;/** h Save the pointer of the header node. * p points to the previous node of the current node, and * s points to the current node */
Int I;/* counter */
If (h = (stud *) malloc (sizeof (stud) = NULL)/* allocate space and detect */
{
Printf ("memory cannot be allocated! ");
Exit (0 );
}
H-> name [0] = '\ 0';/* empty the data field of the header node */
H-> link = NULL;/* Leave the chain field of the header node blank */
P = h;/* p points to the header node */
For (I = 0; I
{
If (s = (stud *) malloc (sizeof (stud) = NULL)/* allocate new storage space and detect */
{
Printf ("memory cannot be allocated! ");
Exit (0 );
}
P-> link = s;/* assign the address of s to the chain domain of the node pointed to by p, and connect the nodes pointed to by p and s */
Printf ("Enter the personal name of % d", I + 1 );
Scanf ("% s", s-> name);/* store the name in the data field of the current node s */
S-> link = NULL;
P = s;
}
Return (h );
}
Main ()
{
Int number;/* variable for storing the number of people */
Stud * head;/* head is the pointer to the header Node Address of the single-chain table */
Number = N;
Head = creat (number);/* assign the header address of the newly created single-chain table to head */
}
In this way, you can create a single-chain table containing N individual names. For programs that write dynamic memory allocation, check whether the allocation is successful.