Linked list source code (C language Implementation)

Source: Internet
Author: User

Source code (C language Implementation)

①. Constructing a linked list node

               typedef struct node   //A separate node
                    {
                          int data;//data field
                          struct Node * pnext;//pointer field
                     }node,* PNODE;

          ②. Initialization of the list

             pnode init_list ()
                   {
                            int len;//The length of the linked list
                           int i;//Loop Switch
                            int the value of the val;//node

Pnode Phead = (pnode) malloc (sizeof (node));//build up the knot

if (Null==phead)
{
printf ("List dynamic allocation failed!") Program Terminated! \ n ");
Exit (-1);
}
Else
{
printf ("Please enter the length of the list to create len=");
scanf ("%d", &len);

if (0==len)
{
Phead->pnext = NULL;
return phead; The processing of the empty linked list
}
if (len<0)//For processing with a negative length
{
printf ("The length of the linked list cannot be negative!") \ n ");
Exit (-1);
}

Pnode Ptail = Phead;//ptail points to the tail node
Ptail->pnext = NULL;

for (i=0;i<len;i++)
{
Pnode pnew = (pnode) malloc (sizeof (node));//Create a new node
printf ("Please enter the number of%d nodes val =", i+1);
scanf ("%d", &val);

Pnew->data = val; Assign value
Ptail->pnext = pnew; Connect a new node
Ptail = pnew; Ptail Move back
}
Ptail->pnext = null;//The pointer field of the last node must be empty
return phead; Returns the head node used to represent the entire list.


}
}

Linked list source code (C language Implementation)

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.