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)