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

Source: Internet
Author: User
Tags assert

In the previous blog, we had an article devoted to the contents of a one-way list. So what's the difference between the list we discussed today and the list we discussed last time? That's the point."Loops"above.    With loops, it means that we can start working from any of the linked list nodes and that we can access the data from any of the linked list nodes, which is the advantage of the loop.    So what is the difference between a circular unidirectional list in the implementation process? 1) Print linked list data [CPP] View plain copyvoidPrint_data (Constlink_node*Plinknode) {Link_node* Pindex =NULL; if(NULL = =Plinknode)return; printf ("%d\n", plinknode->data); Pindex= plinknode->Next;  while(Plinknode! =pindex) {printf ("%d\n", pindex->data); Pindex= PindexNext; In the past, we found that the end of the printed data is to determine if the pointer is null, because this is a circular list, so there is a change. The original condition (NULL! = Plinknode) has also been modified here (plinknode! =pindex).    Functions that also need to be modified are the Find function, the count statistic function. 2) Insert data [CPP] View plain copystatus insert_data (link_node* * Pplinknode,intdata) {Link_node*Pnode; if(NULL = =Pplinknode)returnFALSE; if(NULL = = *Pplinknode) {Pnode=Create_link_node (data); ASSERT (NULL!=Pnode); Pnode->next =Pnode; *pplinknode =Pnode; returnTRUE; }        if(NULL! = Find_data (*Pplinknode, data)) returnFALSE; Pnode=Create_link_node (data); ASSERT (NULL!=Pnode); Pnode->next = (*pplinknode)Next; (*pplinknode)->next =Pnode; returnTRUE; The Insert function here changes in two places: a) If there is no node in the original list, then the list node needs to point to itself b) if the linked list node originally exists, then only need to add a data after the current list node, and modify the two-direction pointers3) Delete data [CPP] View plain copystatus delete_data (link_node* * Pplinknode,intdata) {Link_node* Pindex =NULL; Link_node* prev =NULL; if(NULL = = Pplinknode | | NULL = = *Pplinknode)returnFALSE; Pindex= Find_data (*Pplinknode, data); if(NULL = =pindex)returnFALSE; if(Pindex = = *Pplinknode) {          if(Pindex = = pindex->next) {              *pplinknode =NULL; }Else{prev= pindex->Next;  while(Pindex! = prev->next) Prev= prev->Next; Prev->next = pindex->Next; *pplinknode = pindex->Next; }      }Else{prev= pindex->Next;  while(Pindex! = prev->next) Prev= prev->Next; Prev->next = pindex->Next;      } free (Pindex); returnTRUE; As with adding data, there are two changes to delete data: a) If there is only one data left in the current list node, the deletion needs to be set to null B) when the data is deleted, the first data of the current data is required, and then the data can be traversed from the current deleted In addition to the time need to focus on determining whether the deleted data is the head node data of the linked list

01-(2) data structure-one-step-one-step write algorithm (cyclic one-way 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.