Two-way linked list is also called double linked list, which is a kind of linked list, and it has two pointers in each data node, pointing to direct successor and direct precursor respectively. Therefore, starting from any node in a two-way list, it is easy to access its predecessor nodes and subsequent nodes. In general we construct bidirectional cyclic lists.
(1) Define the basic structure of the bidirectional linked list
Copy Code code as follows:
typedef struct _DOUBLE_LINK_NODE
{
int data;
struct _double_link_node* prev;
struct _double_link_node* next;
}double_link_node;
(2) Create bidirectional linked list nodes
Copy Code code as follows:
double_link_node* create_double_link_node (int value)
{
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;
return pdlinknode;
}
(3) Delete two-way linked list
Copy Code code as follows:
void Delete_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 two-way list
Copy Code code as follows:
double_link_node* find_data_in_double_link (const double_link_node* pdlinknode, int data)
{
double_link_node* pnode = NULL;
if (NULL = = Pdlinknode)
return NULL;
Pnode = (double_link_node*) Pdlinknode;
while (NULL!= pnode) {
if (data = = Pnode->data)
return pnode;
Pnode = Pnode->next;
}
return NULL;
}
(5) Insert data in bidirectional linked list
Copy Code code as follows:
STATUS Insert_data_into_double_link (double_link_node** ppdlinknode, int data)
{
Double_link_node* Pnode;
Double_link_node* Pindex;
if (NULL = = Ppdlinknode)
return FALSE;
if (NULL = = *ppdlinknode) {
Pnode = Create_double_link_node (data);
ASSERT (NULL!= pnode);
*ppdlinknode = Pnode;
(*ppdlinknode)->prev = (*ppdlinknode)->next = NULL;
return TRUE;
}
if (NULL!= find_data_in_double_link (*ppdlinknode, data)
return FALSE;
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;
return TRUE;
}
(6) Delete data in bidirectional linked list
Copy Code code as follows:
STATUS Delete_data_from_double_link (double_link_node** ppdlinknode, int data)
{
Double_link_node* Pnode;
if (NULL = = Ppdlinknode | | NULL = = *ppdlinknode)
return FALSE;
Pnode = Find_data_in_double_link (*ppdlinknode, data);
if (NULL = = Pnode)
return FALSE;
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);
return TRUE;
}
(7) Statistics of the number of data in two-way linked list
Copy Code code as follows:
int Count_number_in_double_link (const double_link_node* Pdlinknode)
{
int count = 0;
double_link_node* Pnode = (double_link_node*) Pdlinknode;
while (NULL!= pnode) {
Count + +;
Pnode = pnode->next;
}
return count;
}
(8) Printing data in two-way linked lists
Copy Code code as follows:
void Print_double_link_node (const double_link_node* Pdlinknode)
{
double_link_node* Pnode = (double_link_node*) Pdlinknode;
while (NULL!= pnode) {
printf ("%d\n", pnode->data);
Pnode = Pnode->next;
}
}
Today we are talking about the two-way linked list is not circular, we can consider if you change to a circular two-way linked list, how should write? If it is an orderly circular two-way linked list, how to write?