01-(2) data structure-one-step-one-step write algorithm (one-way linked list)

Source: Internet
Author: User

In some cases, the data in memory is not contiguous. At this point, we need to add a property to the data structure that will record the address of one of the following.    With this address, all the data is strung up like a chain, and the address attribute acts as a threading link. What are the advantages of the list structure compared to the more common linear structure? We can summarize: (1It is easy to create a single node, and normal linear memory usually needs to set the size of the data when it is created (2the deletion of the node is very convenient and does not need to move the remaining data like a linear structure (3the access of the node is convenient and can be accessed by loop or recursive method, but the average access efficiency is lower than that of linear table. So in practical applications, how is the linked list designed? We can design a simple int linked list based on the INT data type: ( 1 Design The data structure of the linked list [CPP] View plain copytypedefstruct_link_node {intdata; struct_link_node*Next;      }link_node; ( 2 ) Create linked list [CPP] View plain Copylink_node* Alloca_node (intvalue) {Link_node* Plinknode =NULL; Plinknode= (link_node*) malloc (sizeof(Link_node)); Plinknode->data =value; Plinknode->next =NULL; returnPlinknode; } ( 3 ) Delete list [CPP] View plain copyvoidDelete_node (link_node**Pnode) {Link_node**Pnext; if(NULL = = Pnode | | NULL = = *Pnode)return ; Pnext= & (*pnode)Next; Free (*Pnode);   Delete_node (Pnext); } ( 4 ) List insert Data [CPP] View plain copystatus _add_data (link_node* * Pnode, link_node*Pdatanode) {      if(NULL = = *Pnode) {          *pnode =Pdatanode; returnTRUE; }            return_add_data (& (*pnode),Next, Pdatanode); } STATUS Add_data (Constlink_node** Pnode,intvalue) {Link_node*Pdatanode; if(NULL = = *Pnode)returnFALSE; Pdatanode=Alloca_node (value); ASSERT (NULL!=Pdatanode); return_add_data ((link_node**) Pnode, Pdatanode); } ( 5 ) Delete data [CPP] View plain copystatus _delete_data (link_node* * Pnode,intvalue) {Link_node*Plinknode; if(NULL = = (*pnode),next)returnFALSE; Plinknode= (*pnode)Next; if(Value = = plinknode->data) {          (*pnode)->next = plinknode->Next;          Free (Plinknode); returnTRUE; }Else{          return_delete_data (& (*pnode),Next, value); }} STATUS Delete_data (Link_node* * Pnode,intvalue) {Link_node*Plinknode; if(NULL = = Pnode | | NULL = = *Pnode)returnFALSE; if(Value = = (*pnode),data) {Plinknode= *Pnode; *pnode = plinknode->Next;          Free (Plinknode); returnTRUE; }                   return_delete_data (Pnode, value); } ( 6 ) Find Data [CPP] View plain Copylink_node* Find_data (ConstLink_node* Plinknode,intvalue) {      if(NULL = =Plinknode)returnNULL; if(Value = = plinknode->data)return(link_node*) Plinknode; returnFind_data (plinknode->Next, value); } ( 7 ) Print Data [CPP] View plain copyvoidPrint_node (Constlink_node*Plinknode) {      if(Plinknode) {printf ("%d\n", plinknode->data); Print_node (Plinknode-next); }} ( 8 ) Statistics [CPP] View plain copyintCount_node (Constlink_node*Plinknode) {      if(NULL = =Plinknode)return 0; return 1+ Count_node (plinknode->next); }  

01-(2) data structure-one-step-one-step write algorithm (one-way linked list)

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.