一步一步写算法(之单向链表)(二)

2014-11-23 23:33:48 · 作者: · 浏览: 18
ue);

}

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)查找数据

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)打印数据

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)统计数据

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);

}