struct link *f(struct link *head) {
// Do work...
// is found struct link *target
return target;
}
struct link *delete_link(struct link *head, struct link *target) {
if (head == target) {
struct link *tmp = head;
struct link *new_head = tmp->next;
return new_head;
}
struct link *curr = head;
while (curr->next != target) {
struct link *tmp = curr;
curr = curr->next;
}
curr->next = target->next
free(target);
return head;
}
int main(int argc, char *argv[])
{
struct link {
// data
struct link *next;
};
// Construct list....
struct link *head = construct_list(1, 2, 3, 4);
// Do stuff...
struct link *target = f(list);
// Found a deletion target...
head = delete_link(target);
return 0;
}
1条答案
按热度按时间qvtsj1bj1#
对于“单向链接”列表,您需要为列表中的“下一个”项维护的数据结构。
如果在你的例子中(看起来你是),限制使用一个返回指向这个链接的指针的函数,那么你可以循环并相应地删除。
例如: