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

Source: Internet
Author: User
Tags assert

in the previous blog we introduced a one-way list. So the two-way list we're introducing today, as the name implies, is that the data itself has two-way pointers to the left and right. The two-way linked list compares one-way linked list, mainly has the following several characteristics: (1) has a bidirectional pointer in the data structure (2when inserting data, you need to consider the direction of the operation (3Similarly, the deletion of data is also a need to consider the operation of the front and back direction so, what should a non-circular doubly linked list do? We can try it ourselves: ( 1 ) Define the basic structure of the doubly linked list [CPP] View plain copytypedefstruct_double_link_node {intdata; struct_double_link_node*prev; struct_double_link_node*Next;      }double_link_node; ( 2 Create a doubly linked list node [CPP] View plain Copydouble_link_node* Create_double_link_node (intvalue) {Double_link_node* Pdlinknode =NULL; Pdlinknode= (double_link_node*) malloc (sizeof(Double_link_node)); ASSERT (NULL!=Pdlinknode); memset (Pdlinknode,0,sizeof(Double_link_node)); Pdlinknode->data =value; returnPdlinknode; } ( 3 ) Delete the doubly linked list [CPP] View plain copyvoidDelete_all_double_link_node (double_link_node**Pdlinknode) {Double_link_node*Pnode; if(NULL = = *Pdlinknode)return ; Pnode= *Pdlinknode; *pdlinknode = pnode->Next;      Free (pnode);  Delete_all_double_link_node (Pdlinknode); } ( 4 find data in a doubly linked list [CPP] View plain Copydouble_link_node* Find_data_in_double_link (ConstDouble_link_node* Pdlinknode,intdata) {Double_link_node* Pnode =NULL; if(NULL = =Pdlinknode)returnNULL; Pnode= (double_link_node*) Pdlinknode;  while(NULL! =Pnode) {          if(Data = = Pnode->data)returnPnode; Pnode= PnodeNext; }            returnNULL; } ( 5 ) Insert data in a doubly linked list [CPP] View plain copystatus insert_data_into_double_link (double_link_node* * Ppdlinknode,intdata) {Double_link_node*Pnode; Double_link_node*Pindex; if(NULL = =Ppdlinknode)returnFALSE; if(NULL = = *Ppdlinknode) {Pnode=Create_double_link_node (data); ASSERT (NULL!=Pnode); *ppdlinknode =Pnode; (*ppdlinknode)->prev = (*ppdlinknode)->next =NULL; returnTRUE; }        if(NULL! = Find_data_in_double_link (*Ppdlinknode, data)) returnFALSE; Pnode=Create_double_link_node (data); ASSERT (NULL!=Pnode); Pindex= *Ppdlinknode;  while(NULL! = pindex->next) Pindex= pindex->Next; Pnode->prev =Pindex; Pnode->next = pindex->Next; Pindex->next =Pnode; returnTRUE; } ( 6 Delete data in a doubly linked list [CPP] View plain copystatus delete_data_from_double_link (double_link_node* * Ppdlinknode,intdata) {Double_link_node*Pnode; if(NULL = = Ppdlinknode | | NULL = = *Ppdlinknode)returnFALSE; Pnode= Find_data_in_double_link (*Ppdlinknode, data); if(NULL = =Pnode)returnFALSE; if(Pnode = = *Ppdlinknode) {          if(NULL = = (*ppdlinknode),next) {              *ppdlinknode =NULL; }Else{              *ppdlinknode = pnode->Next; (*ppdlinknode)->prev =NULL; }        }Else{          if(pnode->next) Pnode->next->prev = pnode->prev; Pnode->prev->next = pnode->Next;      } free (Pnode); returnTRUE; } ( 7 ) Count the number of data in a doubly linked list [CPP] View plain copyintCount_number_in_double_link (Constdouble_link_node*Pdlinknode) {      intCount =0; Double_link_node* Pnode = (double_link_node*) Pdlinknode;  while(NULL! =Pnode) {Count++; Pnode= pnode->Next; }      returncount; } ( 8 ) Print data in a doubly linked list [CPP] View plain copyvoidPrint_double_link_node (Constdouble_link_node*Pdlinknode) {Double_link_node* Pnode = (double_link_node*) Pdlinknode;  while(NULL! =Pnode) {printf ("%d\n", pnode->data); Pnode= PnodeNext; }} Note: Today we discuss the two-way list is non-cyclic, we can consider if you change to a circular doubly linked list, what should be written? If it is an orderly circular doubly linked list, how to write? 

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