Single linked list-------C language implementation of the lead node

Source: Internet
Author: User

1 /*****************************************************2 author:simon_kly version:0.1 date:201705203 Description: Single linked list with lead contact4 Mail:[email protected]5 funcion List:6 *****************************************************/7 8#include <stdio.h>9#include <stdlib.h>Ten  Onetypedefstructnode A { -     intdata; -     structNode *Next; the}node, *Link; -  - /*determine if malloc is performing correctly*/ - voidIs_malloc_ok (Link head) + { -     if(Head = =NULL) +     { Aprintf"malloc error!\n"); atExit (-1); -     } - } -  - /*Create a linked list*/ - voidCreate_link (link *head) in { -*head = (Link)malloc(sizeof(Node)); toIS_MALLOC_OK (*head); +(*head)->next =NULL; - } the  * /*Create a node*/ $ voidCreate_node (Link *New_node)Panax Notoginseng { -*new_node = (Link)malloc(sizeof(Node)); theIS_MALLOC_OK (*new_node); + } A  the /*Insert node End plug*/ + voidinsert_node_tail (link head, link new_node) - { $Link p =NULL; $      -p =head; -  the      while(P->next! =NULL) -     {Wuyip = p->Next; the     } -P->next =New_node; WuNew_node->next =NULL; - } About  $ /*Insert node nod*/ - voidinsert_node_head (link head, link new_node) - { -Link p =NULL; A  +p =head; the  -New_node->next = head->Next; $Head->next =New_node; the } the  the /*Print Node*/ the voidoutput_link (link head) - { inLink p =NULL; the  the  About     if(Head = =NULL) the{//Empty Chain theprintf"Link is empty!\n"); the         return ; +     } -  thep = head->Next;Bayi      while(P! =NULL) the     { theprintf"%d\n", p->data); -p = p->Next; -     } the } the  the /*Empty Chain*/ the voidMake_empty_link (link *head) - { theLink p =NULL; the  thep = (*head)Next;94  the      while((*head)->next! =NULL) the     { the(*head)->next = (*head)->next->Next;98          Free(p); Aboutp = (*head)Next; -     }101 }102 103 /*release linked list*/104 voidRelease_link (link *head) the {106 Make_empty_link (head);107      Free(*head);108*head =NULL;109 } the 111 intMain () the {113     inti; the  theLink head =NULL; theLink New_node =NULL;117 118Create_link (&head);119      -     /*Tail Plug*/121      for(i =0; I <Ten; i++)122     {123Create_node (&new_node);124New_node->data = i +1; the Insert_node_tail (head, new_node);126     }127 Output_link (head); - 129     /*Head Plug*/ theCreate_node (&new_node);131New_node->data = -; the Insert_node_head (head, new_node);133 Output_link (head);134 135     /*release the linked list stage*/136Release_link (&head);137 Output_link (head);138     return 0;139}

No lead node Code Portal: http://www.cnblogs.com/SimonKly/p/6890287.html

It can be seen from the code that the parameter in the function insert* that inserts the node in the code that does not take the lead is a level two pointer, because the direction of the head pointer may change, and the address of the first-level pointer needs to be brought back with a level two pointer.

And in the lead node code in the same insertion node in the function insert* parameter is a first-class pointer and does not use a two-level pointer, because the head node in the list of nodes is a real real existence, in the create* to allocate space to it, but its range has no value, the change is its pointer to the direction of the field, The address of its pointer field can be brought back by itself.

The list of lead nodes is more straightforward to handle than the list of non-leading nodes, regardless of whether the head pointer has a change in direction.

Single linked list-------C language implementation of the lead node

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.