我想编写一个函数,它获取指向链表的头的指针,并从列表中删除每个第二个元素的列表。这份名单是类型元素的连接的元件:c中的指针删除链表中每第二个元素的函数
typedef struct element{
int num;
struct element* next;
}element;
我是新来的所有这些指针运算,所以我不知道我写正确的:
void deletdscnds(element* head) {
element* curr;
head=head->next; //Skipping the dummy head//
while (head!=NULL) {
if (head->next==NULL)
return;
else {
curr=head;
head=head->next->next; //worst case I'll reach NULL and not a next of a null//
curr->next=head;
}
}
}
我不停地变化着它因为我一直在发现错误。你能指出任何可能的错误吗?
哎呀!在你这样做了一段时间后,你会在泄露的记忆中站立得很深。你还没有删除任何东西......你刚刚失去了它。 – dmckee 2012-07-19 17:29:18
我应该在放手之前使用“免费”功能吗? – Jozef 2012-07-19 17:30:55
在删除curr之前,您必须获取curr-> next的值。 – 2012-07-19 17:31:31