A simple node structure is expressed as:
Struct Note
{
INT data; / * Data member can be multiple different types of data * /
Struct Note * Next; / * Pointer variable member can only be - * /
}
Illustration of a simple one-way lin list
1. The linked list is a structural, pointer-based-type application, which is a single-directional scalable linked list composed of head, middle, and tail plurality of chains, and the link on the chain table is called node.
2. The data for each node can be used - a structural representation, which consists of two parts: data members and structural pointer variables.
3. Data member stores the data required by the user, and the structural pointer variable member is used to connect (pointing) under - a node, and the pointer variable is called a structural pointer variable because each structural pointer variable member points to the same structure. .
4. The length of the linked list is dynamic. When it is necessary to establish a node, apply to the system to dynamically allocate - a storage space, so that there is a new node generation until the structural pointer variable points to empty (NULL). Application Dynamic Allocation - The representation of the storage space is:
(struct note *) Malloc (Struct Note))
Establishment of the list
During the establishment of the chain, first build the first node, then constantly
Add a new node in its tail until there is no need to have a new node, that is, the tail pointer points to
NULL.
There is a structural pointer variable Struct Note * P, * p1, * head;
HEAD: Used to sign the chain head;
P: During the chain establishment process, P is always constantly accepting new nodpoint addresses for system dynamic allocation.
P1-> Next: Store the address of the new node.
The steps created by the linked list:
Step 1: Establish the first node
Struct Node
{
Int data;
Struct Node * Next;
}
Struct Note * P, * p1, * head;
HEAD = P1 = P = (struct node *) malloc (Struct Node);
Step 2:
Assign the first node member DATA and generate a second node
Scanf ("% D", & P-> DATA); / * Input 10 * /
P = (struct node *) malloc (SIZEOF (Struct Node);
Step 3: Connect the first node with the second node
P1-> next = P;
Step 4: Generate a third node
P1 = P;
Scanf ("% d", & p-> data); / * Enter 8 * /
P = (struct node *) malloc (SIZEOF (Struct Node);
The following steps are repeated third, four steps until it is given - the end condition, no new junction, there must be
P-> next = null; it represents the tail node.
Code
#include
#include
#define len sizeof (struct node)
Struct Node
{
Int data;
Struct Node * Next;
}
Main ()
{struct node * p, * pl, * head;
Head = p = (struct node *) malloc (len);
Scanf ("% d", & p-> data); / * data member of the head knot * /
While (P-> Data! = 0) / * gives 0 end conditions, exit loop * /
{PL = P;
P = (struct node *) malloc (len);
Scanf ("% d", & p-> data); / * Intermediate node data member * /
PL-> Next = P; / * Middle Node pointer member value * /}
P-> Next = null; / * Pointer member value of the tailpoint * /
P = head; / * Link list Show * /
Printf ("Lin table data member is:");
While (p-> next! = null)
{
Printf ("% D", P-> DATA);
P = P-> next;
}
Printf ("% d \ n", p-> data);
}