Two kinds of insertion methods for chained storage of single-linked lists

Source: Internet
Author: User

/* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.