c - 从 C 中的节点删除特定成员

标签 c

<分区>

这很乱,所以如果您没有时间,请不要打扰。我已尽力解释我的代码中的每个函数如何发挥最佳作用。所以我的问题是函数 Q_drop 我无法正常工作,其他一切(代码的所有其他部分)都运行良好。

所以函数 Q_drop 需要做以下事情:

实现函数 Q_drop,从队列中删除给定的特定成员(由 ID 标识),并释放为其分配的内存。这个队列成员可以位于队列中的任何位置,并且在移除之后,队列应该保持可操作,即所有指针都应该相应地更新。并且 My_Queue->last 指针需要指向最后一个成员。

如果找到并删除了具有匹配 ID 的物种,该函数返回 1;如果没有匹配且没有删除任何内容,则返回 0。每次调用应该只删除一个队列成员:如果一个物种多次出现在队列中,则只删除第一个条目。

所以我的问题是,如果匹配的节点是最后一个节点,我如何将指针移动到前一个节点?我收到的检查器错误是“My_Queue->last pointer does not point to the last member”。

我的代码:

struct animal {
char id[7];  // animal ID: 6 characters + '\0'
char *species;  // species of animal, allocated dynamically
struct animal *next;  // next animal in linked list
};

/* For storing the first and last item in linked list
 * If list is empty, both <first> and <last> are NULL
 * If list has one element, <first> and <last> point to the same place
 */
struct my_queue {
    struct animal *first;
    struct animal *last;
};

const struct zoo {
const char *id;
const char *species;
} animals[] = {
    {"123456", "Dog" },
    {"234567", "Bear" },
    {"777777", "Pink Fairy Armadillo" },
    {"aaaaaaaaa", "Chlamyphorus truncatus" },
    {"666666", "Mosquito" }
};

/* Drops the given animal with animal ID <id> from queue <q>.
 * Only the first instance with matching ID is dropped.
 * 
 * Returns: 1 if something was removed,
 * 0 if nothing was removed, i.e., matching animal ID was not found.
 */

int Q_drop(My_Queue *q, const char *id)
{ 

    struct animal *prev = NULL;
    struct animal *curr = q->first;

    while (curr != NULL) {
        if (!strcmp(curr->id, id)) {
        if (prev == NULL) {
            q->first = curr->next;
        }
        if(curr->next == NULL){
             //here I cant figure out what to write here tried q->last=prev
        }
        else {
            prev->next = curr->next;
        }
    free(curr->species);
    free(curr);
    return 1;
    }
    else {        
      prev = curr;
      curr = curr->next;
    }
    }
    return 0;
}

/* Allocates and initializes a new queue.
 * 
 * Returns: pointer to an empty queue
 */
My_Queue *Q_init(void)
{
    My_Queue *q = calloc(1, sizeof(My_Queue));
    return q;
}

/* Adds a new member with given animal ID <id> and species <species> to the
 * end of queue <q>.
 * 
 * Returns: 1 if addition was successful, 0 if addition failed. Addition 
  fails,
 * for example if animal ID has more than 6 characters.
 */
int Q_enqueue(My_Queue *q, const char *id, const char *species)
{    
    int n = strlen(id);
    if (n < 7){
        struct animal *new = malloc(sizeof(struct animal));
        strcpy(new->id, id);
        new->species = malloc(strlen(species) + 1);
        strcpy(new->species, species);
        new->next = NULL;
        if(q->first == NULL && q->last == NULL){
            q->first = new;
            q->last = new;
        }
        else{
            q->last->next = new;
            q->last = new;
        }
        return 1;
    }
    else{
        return 0;
    }

}

int main()
{
    /* testing exercise. Feel free to modify this function */

    My_Queue *q = Q_init();

    for (unsigned int i = 0; i < sizeof(animals) / sizeof(struct zoo); i++)                
    {
        if (Q_enqueue(q, animals[i].id, animals[i].species))
            printf("i = %d, firstspecies: %s\n", i, Q_firstSpecies(q));
    }

    Q_drop(q, "777777");

最佳答案

好了,到此为止

if (!strcmp(curr->id, id)) {
    if (prev == NULL) {
        q->first = curr->next;
    } else {
        prev->next = curr->next;
    }

你已经找到了你想要释放的节点,但是你这样做了

curr = curr->next;

在你做之前

free(curr->species);
free(curr);

所以在我看来你没有释放正确的节点,而是释放了它之后的节点。您的代码缩进有点偏离,但我认为您可以删除 curr = curr->next; 因为您稍后在 else 部分对应于 not找到了正确的节点。

int Q_drop(My_Queue *q, const char *id) { 
    struct animal *prev = NULL;
    struct animal *curr = q->first;

    while (curr != NULL) {
        if (!strcmp(curr->id, id)) {
            if (prev == NULL) {
                q->first = curr->next;
            } else {
                prev->next = curr->next;
            }

            free(curr->species);
            free(curr);
            return 1;
        }
        else {        
            prev = curr;
            curr = curr->next;
        }
    }

    return 0;
}

关于c - 从 C 中的节点删除特定成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49065636/

相关文章:

c - 我如何混合使用字符串文字和 float 并将它们连接成 C 中的一个字符串?

使用 fopen(fp ,"wb") 创建用户输入的任何文件的副本

c - 输出从函数返回结构体

c - 查找数字二进制位置

c - Linux 中的 TCP 总线错误

c - 如何在带有 GCC 的 C 程序中的函数之间暂停?

c - 无法退出 while 循环

c - 打印可变参数函数中的数组索引

c - 这个值是如何计算的?

c - 为什么我的 C 程序的输出不正确?