[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Sometimes, data in the memory is not continuous. At this time, we need to add an attribute in the data structure, which records the following data address. With this address, all the data is chained up like a chain, so this address attribute plays the role of threading.
What are the advantages of the linked list structure compared with the ordinary linear structure? We can summarize:
(1) It is very convenient to create a single node. The data size needs to be set during the creation of ordinary linear memory.
(2) Deletion of nodes is very convenient, and the remaining data does not need to be moved like a linear structure.
(3) convenient access to nodes. Arbitrary data can be accessed through loops or recursion, but the average access efficiency is lower than that of linear tables.
In practice, 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
Typedef struct _ LINK_NODE
{
Int data;
Struct _ LINK_NODE * next;
} LINK_NODE;
Typedef struct _ LINK_NODE
{
Int data;
Struct _ LINK_NODE * next;
} LINK_NODE;
(2) create a linked list
LINK_NODE * alloca_node (int value)
{
LINK_NODE * pLinkNode = NULL;
PLinkNode = (LINK_NODE *) malloc (sizeof (LINK_NODE ));
PLinkNode-> data = value;
PLinkNode-> next = NULL;
Return pLinkNode;
}
LINK_NODE * alloca_node (int value)
{
LINK_NODE * pLinkNode = NULL;
PLinkNode = (LINK_NODE *) malloc (sizeof (LINK_NODE ));
PLinkNode-> data = value;
PLinkNode-> next = NULL;
Return pLinkNode;
}
(3) Delete A linked list
Void delete_node (LINK_NODE ** pNode)
{
LINK_NODE ** pNext;
If (NULL = pNode | NULL = * pNode)
Return;
PNext = & (* pNode)-> next;
Free (* pNode );
Delete_node (pNext );
}
Void delete_node (LINK_NODE ** pNode)
{
LINK_NODE ** pNext;
If (NULL = pNode | NULL = * pNode)
Return;
PNext = & (* pNode)-> next;
Free (* pNode );
Delete_node (pNext );
} (4) insert data to the linked list
STATUS _ add_data (LINK_NODE ** pNode, LINK_NODE * pDataNode)
{
If (NULL = * pNode ){
* PNode = pDataNode;
Return TRUE;
}
Return _ add_data (& (* pNode)-> next, pDataNode );
}
STATUS add_data (const LINK_NODE ** pNode, int value)
{
LINK_NODE * pDataNode;
If (NULL = * pNode)
Return FALSE;
PDataNode = alloca_node (value );
Assert (NULL! = PDataNode );
Return _ add_data (LINK_NODE **) pNode, pDataNode );
}
STATUS _ add_data (LINK_NODE ** pNode, LINK_NODE * pDataNode)
{
If (NULL = * pNode ){
* PNode = pDataNode;
Return TRUE;
}
Return _ add_data (& (* pNode)-> next, pDataNode );
}
STATUS add_data (const LINK_NODE ** pNode, int value)
{
LINK_NODE * pDataNode;
If (NULL = * pNode)
Return FALSE;
PDataNode = alloca_node (value );
Assert (NULL! = PDataNode );
Return _ add_data (LINK_NODE **) pNode, pDataNode );
} (5) delete data
STATUS _ delete_data (LINK_NODE ** pNode, int value)
{
LINK_NODE * pLinkNode;
If (NULL = (* pNode)-> next)
Return FALSE;
PLinkNode = (* pNode)-> next;
If (value = pLinkNode-> data ){
(* PNode)-> next = pLinkNode-> next;
Free (pLinkNode );
Return TRUE;
} Else {
Return _ delete_data (& (* pNode)-> next, value );
}
}
STATUS delete_data (LINK_NODE ** pNode, int value)
{
LINK_NODE * pLinkNode;
If (NULL = pNode | NULL = * pNode)
Return FALSE;
If (value = (* pNode)-> data ){
PLinkNode = * pNode;
* PNode = pLinkNode-> next;
Free (pLinkNode );
Return TRUE;
}
Return _ delete_data (pNode, value );
}
STATUS _ delete_data (LINK_NODE ** pNode, int value)
{
LINK_NODE * pLinkNode;
If (NULL = (* pNode)-> next)
Return FALSE;
PLinkNode = (* pNode)-> next;
If (value = pLinkNode-> data ){
(* PNode)-> next = pLinkNode-> next;
Free (pLinkNode );
Return TRUE;
} Else {
Return _ delete_data (& (* pNode)-> next, value );
}
}
STATUS delete_data (LINK_NODE ** pNode, int value)
{
LINK_NODE * pLinkNode;
If (NULL = pNode | NULL = * pNode)
Return FALSE;
If (value = (* pNode)-> data ){
PLinkNode = * pNode;
* PNode = pLinkNode-> next;
Free (pLinkNode );
Return TRUE;
}
Return _ delete_data (pNode, value );
}
(6) Search for Data
LINK_NODE * find_data (const LINK_NODE * pLinkNode, int value)
{
If (NULL = pLinkNode)
Return NULL;
If (value = pLinkNode-> data)
Return (LINK_NODE *) pLinkNode;
Return find_data (pLinkNode-> next, value );
}
LINK_NODE * find_data (const LINK_NODE * pLinkNode, int value)
{
If (NULL = pLinkNode)
Return NULL;
If (value = pLinkNode-> data)
Return (LINK_NODE *) pLinkNode;
Return find_data (pLinkNode-> next, value );
} (7) print data
Void print_node (const LINK_NODE * pLinkNode)
{
If (pLinkNode ){
Printf ("% d \ n", pLinkNode-> data );
Print_node (pLinkNode-> next );
}
}
Void print_node (const LINK_NODE * pLinkNode)
{
If (pLinkNode ){
Printf ("% d \ n", pLinkNode-> data );
Print_node (pLinkNode-> next );
}
} (8) Statistical data
Int count_node (const LINK_NODE * pLinkNode)
{
If (NULL = pLinkNode)
Return 0;
Return 1 + count_node (pLinkNode-> next );
}
Int count_node (const LINK_NODE * pLinkNode)
{
If (NULL = pLinkNode)
Return 0;
Return 1 + count_node (pLinkNode-> next );
}