/* head interpolation to create a single-chain example */
void Createlisthead (linklist *l, int n)
{
Linklist p;
int i;
Srand (Time (0)); Initialize random number Seed
*l = (linklist) malloc (sizeof (Node));
(*l)->next = NULL;
for (i=0; i < n; i++)
{
p = (linklist) malloc (sizeof (Node)); Create a new node
P->data = rand ()%100+1;
P->next = (*l)->next;
(*l)->next = p;
}
}
/* tail interpolation to create a single-chain show */
void Createlisttail (linklist *l, int n)
{
Linklist p, R;
int i;
Srand (Time (0));
*l = (linklist) malloc (sizeof (Node));
R = *l;
for (i=0; i < n; i++)
{
p = (node *) malloc (sizeof (node));
P->data = rand ()%100+1;
R->next = p;
r = P; Note: Beginners may be difficult to understand this sentence, the key explanation.
}
R->next = NULL;
}
The last two sentences are actually more variables that add a node data type, and use it to point to the last node. And then in the next loop, R helps to point the last node's next pointer to the new node P
Two kinds of insertion methods for chained storage of single-linked lists