In some cases, the data in memory is not contiguous. At this point, we need to add a property to the data structure that will record the address of one of the following. With this address, all the data is strung up like a chain, and the address attribute acts as a threading link.
What are the advantages of the list structure compared to the more common linear structure? We can summarize:
(1) The creation of a single node is very convenient, normal linear memory is usually created when you need to set the size of the data
(2) node deletion is very convenient, do not need to move the remaining data like a linear structure
(3) The access of the node is convenient and can be accessed by loop or recursive method, but the average access efficiency is lower than the linear table
So in the practical application, how is the linked list designed? We can design a simple int linked list with the INT data type as the basis:
(1) Design the data structure of the linked list
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; }
(3) Delete the list
void delete_node (Link_node** pnode) { link_node** pnext; if (NULL == pnode | | null == *pnode) return ; pNext = & (*pnode)->next; free (*pnode); Delete_node (pnext); }
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 _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); }
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) Printing data
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); }
Data structure and algorithm (three)--unidirectional linked list