从链接列表中删除元素

用户名

我有一些工作的东西,但仍然无法删除最后一个元素,它只是在其上留下垃圾而不是删除它。我知道这段代码看起来不太好,但是根据我现在的知识,这就是我可以做的所有事情:x。因此,如果有人可以帮助我删除上一个元素的工作或告诉我我做错了什么,那将是很好的

struct item {
    char item_name[30];
    char item_state[30];
    float item_price;
    char item_status[30];
    float item_price_if_not;
    struct item *next;
};
struct client {
    char client_name[30];
    char client_last_name[30];
    struct item *item_data;
    struct client *next;
};

void DeleteClient2(struct client *temp,struct client **head) {
    struct client *prev=*head;
    struct client *current = *head;
    struct item *currentitem = (*head)->item_data,*save;
    if(temp== *head) {
        while(currentitem != NULL) {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        free(temp);
        temp->item_data = NULL;
        (*head) = (*head)->next;
    }
    else if(temp->next == NULL) {
        while(currentitem != NULL)  {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        temp->item_data = NULL;
        free(temp);
    }
    else if(temp != *head && temp->next != NULL) {
        while(prev->next != temp) {
            prev=prev->next;
        }
        prev->next = temp->next;
        while(currentitem != NULL) {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        temp->item_data = NULL;
        free(temp);
        temp=temp->next;
    }
}
Noelicus

您要从头部而不是从临时删除项目数据。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章