根据给定的数值,遍历链表,删除对应节点有两种可能:
①:找到一个节点的data域为给定的数值,删除这个节点,函数返回。实现代码如下:
/*Delete one node of given value
void DeleteOneNodeFromList(Node **pList,int value)
{
Node *pCurrent;
while((pCurrent=*pList)!=NULL && pCurrent->data != value)
{
pList = &(pCurrent->next);
}
if(NULL == pCurrent)
return ;
*pList = pCurrent->next;
free(pCurrent);
return ;
}
*/
②:删除链表中所有数据域data值为给定值value的节点。实现代码如下:
/*Delete all of the node whose data is equal to value
**
**/
void DeleteDataFromList(Node **pList,int value)
{
Node *pCurrent;
while((pCurrent = *pList) != NULL)
{
if(value == pCurrent->data)
{
*pList = pCurrent->next;
free(pCurrent);
}
else
{
pList = &(pCurrent->next);
}
}
}
时间: 2024-10-23 08:41:35