As a collection of similar data, the array gives us a lot of convenience in the program design and increases the flexibility. But the array also has some drawbacks. The size of an array is defined in advance and cannot be adjusted in the program, so that in programming it is sometimes necessary to have 3 0 size arrays for different problems, and sometimes 5 0 array sizes,
Difficult to unify. We can only define arrays based on the maximum possible requirements, which often result in a waste of some storage space.
We want to construct a dynamic array that can resize the array at any time to meet the needs of different problems. The list is the dynamic array we need. It is in the process of execution of the program in accordance with the need for data storage to the system requirements to request storage space, in no way constitute a waste of storage area.
The linked list is a kind of complex data structure, and the relationship between the data makes the list divided into three kinds: single linked list, circular chain list, bidirectional linked list, and the following will be introduced.
7.4.1 Single Linked list
Figure 7-3 is the structure of a single linked list.
A single linked list has a head node H e A D that points to the first address of the linked list in memory. The data type of each node in the list is a struct type. A node has two members: an integral member (the actual data to be saved) and a pointer to the next struct type node (in fact, the single linked list is a dynamic array for storing integer data). Linked list According to this structure, access to each node needs to be found from the head of the linked list, and the address of subsequent nodes is given by the current node. Regardless of which node is accessed in a table, you need to start from the head of the list and look backwards in order. The tail node of the linked list has no subsequent nodes, its pointer field is NULL, and writes as N U l L.
Figure 7-3 also gives such a layer of meaning, the list of nodes in the memory of the storage address is not continuous, the address of each node is in need of the system to apply for allocation, the system according to the current situation of memory, can be divided into consecutive addresses, can also be a jump to the allocation of addresses.
Look at the data structure definition of the linked list node:
struct node
{
int num;
struct node *p;
} ;
In the definition of a linked list node, except for members of an integral type, member P is a pointer to exactly the same type of node.
A very special point in the data structure of a linked list node is that the data type of the pointer field in the structure body uses a data type that is not defined successfully. This is the only data structure in C that can be defined in the first use.
• The process of creating a single linked list has the following steps:
1) Define the data structure of the linked list.
2 Create an empty table.
3 Use the M a l l o C () function to assign a node to the system application.
4 assigns the pointer member of the new node to null. If the table is empty, connect the new node to the header, or if the table is not empty, the new
node is attached to the end of the table.
5 to determine whether there are subsequent nodes to access the linked list, if there is a transfer to 3, or end.
• The output process of a single linked list has the following steps
1) Locate the header of the table.
2 If the table is not empty, the output node's value member, is an empty table to exit.
3 Tracking the growth of the list, that is to find the next node's address.
4) go to 2).
[Example 7-5] creates a single linked list that holds a positive integer (Input-9 9 9) and prints the output.
#include <stdlib.h>/package * with MA l l o C () header file * *
#include <stdio.h>
struct Node/* Link Table structure */
{
int num;
struct node *next;
} ;
M a i n ()
{
struct node *creat (); /* Function declaration */
void print ();
struct node *head; /* Define head pointer */
head=null;/* to build an empty table * *
Head=creat (head)/* Create a single linked list * *
Print (head)///Printed single link list * *
}
/******************************************/
struct Node*creat (structnode*head) function/number * Returns a pointer of the same type as the node.
{
struct NODE*P1,*P2;
P1=p2= (structnode*) malloc (sizeof (structnode)); Apply/* NEW Node/*
scanf ("%d", &p1->num);/* Input node's value * *
p1->next=null;/* the pointer of the new node to empty */
while (p1->num>0)/* The value of the input node is greater than 0*/
{
if (head==null) head=p1;/* empty table, Access table head * *
elsep2->next=p1;/* Non-empty table, received the end of the table * *
P2=P1;
p1= (structnode*) malloc (sizeof (Structnode)), Shen/please * Next new node * *
scanf ("%d", &p1->num);/* Input node's value * *
}
Return head;/* the head pointer of the linked list.
}
/*******************************************/
void print (struct node*head) output/* The value of each node of the list with head
{
struct node *temp;
temp=head;/* gets the head pointer of the linked list.
while (temp!=null)/* As long as the non-empty table */
{
printf ("%6d", temp->num);/* Output list node value * *
temp=temp->next;/* Tracking Chain List growth * *
}
}
The head pointer of a linked list is a very important parameter during the creation of a linked list. Because the output of the list and lookup are to start from the head of the list, so the linked list after the successful creation, to return a linked table header node address, that is, the head pointer.