First, the establishment of a single linked list
With the basis of dynamic memory allocation, it is not difficult to implement the linked list.
A linked list is a data structure that stores linear table elements in a set of arbitrary storage units.
Linked list is divided into single linked list, two-way linked list and circular chain list. Let's talk about the single linked list first.
The so-called single linked list, refers to the data contacts are one-way arrangement. A single linked table node whose structure type is divided into two parts:
1, data fields: to store their own data
2, a chain domain or called a pointer field: A pointer that stores the next node address or points to its immediate successor.
Cases:
typedef struct NODE
{
Char name[20];
struct node *link;
}stud;
This defines the structure of a single linked list, where Char name[20] is a character array for storing names, and pointer *link is a pointer to store its immediate successor.
After defining the structure of the linked list, as long as the program runs in love with the data field to store the appropriate data, if there are subsequent nodes, the link field to its immediate successor, if not, then null.
Here is a complete procedure for establishing a single linked list with a table header (if not stated, the following list is all with a header).
#include <stdio.h>
#include <malloc.h>/* header file containing dynamic memory allocation functions/*
#define N/*n for Number * *
typedef struct NODE
{
Char name[20];
struct node *link;
}stud; Stud * creat (int n)/* Create a single linked list of functions, parameter n for the number of people * *
{
Stud *p,*h,*s; /* *h hold the pointer of the header node, *p point to the previous node of the current node, *s point to the current node.
int i; /* Counter */
if ((h= (Stud *) malloc (sizeof (stud)) ==null)/* Allocate space and detect */
{
printf ("Cannot allocate memory space!");
Exit (0);
}
H->name[0]= ' "; /* Set the data field of the header node to empty */
h->link=null; * * Put the header node in the Chain field empty * *
P=h; /*p point to the Table head node * *
for (i=0;i<n;i++)
{
if (s= (stud *) malloc (sizeof (stud)) ==null)/* Allocate new storage space and detect/*
{
printf ("Cannot allocate memory space!");
Exit (0);
}
p->link=s; /* Assign the address of S to the chain of nodes to which p is pointing, thus connecting the nodes to which P and s are pointing.
printf ("Please enter the name of%d person", 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; /* Save the number of variables *
Stud *head; /*head is a pointer to the address of the header node of a single linked list.
Number=n;
Head=creat (number); * * Assign the newly created single link table header address to head*/
}
This makes it possible to create a single linked list that contains n individual names.
Write dynamic memory allocation programs should be aware, please try to test the success of the allocation.