C 结构中的链表

C 结构中的链表,c,pointers,struct,linked-list,C,Pointers,Struct,Linked List,我在更改结构中指针的地址时遇到问题。 函数接收服务类型(指向结构的指针)和ID。该服务包含类型单元的链接列表(指向单元结构的指针),我希望找到具有给定ID的单元并将其从列表中删除。问题是-当我回到原来的函数时,service->listedpartment仍然指向与以前相同的方向 ApartmentServiceResult serviceDeleteById(ApartmentService service, int id) { Node previous = NULL; No

我在更改结构中指针的地址时遇到问题。 函数接收服务类型(指向结构的指针)和ID。该服务包含类型单元的链接列表(指向单元结构的指针),我希望找到具有给定ID的单元并将其从列表中删除。问题是-当我回到原来的函数时,service->listedpartment仍然指向与以前相同的方向

ApartmentServiceResult serviceDeleteById(ApartmentService service, int id) {
    Node previous = NULL;
    Node after = NULL;
    Node current = service->listedApartments;
    while (current != NULL) {
        after = current->next;
        if (current->id == id) {
            apartmentDestroy(current->apartment); //deletes the apartment
            free(current);
            if (previous == NULL) {
                service->listedApartments = after;
            } else {
                previous->next = after;
                service->listedApartments=previous;
            }
            return APARTMENT_SERVICE_SUCCESS;
        }
        previous = current;
        current = current->next;
    }
    return APARTMENT_SERVICE_NO_FIT;
}

你确定你在传递推荐信吗

这一行是按值传递结构,这意味着函数将复制并修改传递的参数,而不改变外部的值

ApartmentServiceResult serviceDeleteById(ApartmentService service, int id) {
要通过引用传递,必须显式放置*并相应地处理引用:

ApartmentServiceResult serviceDeleteById(ApartmentService * service, int id) {

除非你对typedef做了一些改动,否则我想这可能就是问题所在。

你确定你在传递引用吗

这一行是按值传递结构,这意味着函数将复制并修改传递的参数,而不改变外部的值

ApartmentServiceResult serviceDeleteById(ApartmentService service, int id) {
要通过引用传递,必须显式放置*并相应地处理引用:

ApartmentServiceResult serviceDeleteById(ApartmentService * service, int id) {
除非你对typedef施了魔法,否则我想这可能就是问题所在。

删除(在OQ中未显示)typedef,然后只使用
struct xx*
struct yy*
,片段可以减少为:

ApartmentServiceResult serviceDeleteById(struct xx *service, int id) {
struct yy **pp, *p;

for (pp= &service->listedApartments; (p = *pp); pp = &p->next) {
    if (p->id != id) continue;
    apartmentDestroy(p->apartment);
    *pp = p->next; /* steal the pointer */
    free(p);
    return APARTMENT_SERVICE_SUCCESS;
    }
return APARTMENT_SERVICE_NO_FIT;
}
删除(OQ中未显示)typedef,仅使用
struct xx*
struct yy*
,片段可以减少为:

ApartmentServiceResult serviceDeleteById(struct xx *service, int id) {
struct yy **pp, *p;

for (pp= &service->listedApartments; (p = *pp); pp = &p->next) {
    if (p->id != id) continue;
    apartmentDestroy(p->apartment);
    *pp = p->next; /* steal the pointer */
    free(p);
    return APARTMENT_SERVICE_SUCCESS;
    }
return APARTMENT_SERVICE_NO_FIT;
}

在链接列表中,首先要记住的是,每次调用函数时都必须按ref调用,因为您希望在链接列表中进行该操作,并且您的函数
serviceDeleteById
正在按值接收其参数。因此,修改它并尝试运行代码

void del(int d)
{
    struct node *temp,*ptr;
    temp=start;
    while(temp!=NULL)
    {
        if(temp->link->info==d)
        {
            ptr=temp->link;
            temp->link=ptr->link;
            free(ptr);
        }
        temp=temp->link;
    }
}

此代码示例将帮助您编写代码。您可以在链接列表中进行检查。

首先要记住的是,每次调用函数时都必须按ref进行调用,因为您希望在链接列表中进行该操作,并且您的函数
serviceDeleteById
正在按值接收其参数。因此,修改它并尝试运行代码

void del(int d)
{
    struct node *temp,*ptr;
    temp=start;
    while(temp!=NULL)
    {
        if(temp->link->info==d)
        {
            ptr=temp->link;
            temp->link=ptr->link;
            free(ptr);
        }
        temp=temp->link;
    }
}

此代码示例将帮助您编写代码。您可以检查。

@wildplasser-如果您注意到If语句末尾有一个返回。因此,这不应该是问题所在。我想,我被过多的
{}
弄糊涂了。辅助变量的大负载,以及后面的'p=p.next;'@wildplasser-如果您注意到If语句末尾有一个返回。因此,这不应该是问题所在。我想,我被过多的
{}
弄糊涂了。辅助变量的大负载,以及后面的'p=p.next;'。问题是服务已经是指向结构的指针@Denilson MelloYea,让我们看看其他函数,重要的是服务已经是指向结构的指针@Denilson MelloYea,让我们看看其他功能