标签:style color strong io for 代码
利用二级指针删除链表内一个元素,传统的做法是:找到将要删除元素的前一个指针,然后再删除当前元素。代码示例:
void
delete_node( elem_type
x, struct
node* l )
{
????struct
node* p = find_prev ( x, l );
????if ( !p->next ) {
????????printf ( "element %d not in the struct node*\n", x );
????????return;
????}
????struct
node* tmp = p->next;
????p->next = p->next->next;
????free ( tmp );
}
而通过二级指针的方法,则不需要如此。
void
delete_version_2 ( struct
node** head, elem_type
x )
{
????for ( struct
node** curr = head; *curr; ) {
????????struct
node* entry = *curr;
????????if ( entry->elem == x ) {
????????????*curr = entry->next;
????????????free ( entry );
????????} else {
????????????curr = &entry->next;
????????}
????}
}
标签:style color strong io for 代码
原文地址:http://www.cnblogs.com/x5lcfd/p/3854226.html