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

从C中的节点删除特定成员,c,C,这很混乱,所以如果你没有时间,就不要麻烦了。我试着解释代码中的每个函数是如何尽可能地工作的。所以我的问题是我无法使用的函数Q_drop,其他所有部分(代码的所有其他部分)都能正常工作 因此,函数Q_drop需要执行以下操作: 实现函数Q_drop,从队列中删除给定的特定成员(由ID标识),并释放为其分配的内存。此队列成员可以位于队列中的任何位置,移除后,队列应保持可操作性,即所有指针应相应更新。My_Queue->last指针需要指向最后一个成员 如果找到并删除了具有匹配的ID的物种,则函数返回

这很混乱,所以如果你没有时间,就不要麻烦了。我试着解释代码中的每个函数是如何尽可能地工作的。所以我的问题是我无法使用的函数
Q_drop
,其他所有部分(代码的所有其他部分)都能正常工作

因此,函数
Q_drop
需要执行以下操作:

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

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

因此,我的问题是,如果匹配的节点是最后一个节点,如何将指针移动到前一个节点?我从检查器中得到的错误是“我的队列->最后一个指针没有指向最后一个成员”

我的代码:

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");
结构动物{ 字符id[7];//动物id:6个字符+'\0' char*species;//动物种类,动态分配 struct animal*next;//链表中的下一个动物 }; /*用于存储链表中的第一项和最后一项 *如果列表为空,则和都为空 *如果列表有一个元素,并指向同一位置 */ 构造我的队列{ 结构动物*第一; 结构动物*最后; }; 康斯特结构动物园{ 常量字符*id; 常量字符*种; }动物[]={ {“123456”,“狗”}, {“234567”,“熊”}, {“777777”,“粉红仙女犰狳”}, {“aaaaaaa”,“截形衣原体”}, {“666666”,“蚊子”} }; /*从队列中删除具有动物ID的给定动物。 *仅删除具有匹配ID的第一个实例。 * *返回:1如果删除了某些内容, *如果未删除任何内容,即未找到匹配的动物ID,则为0。 */ int Q_drop(我的队列*Q,常量字符*id) { struct animal*prev=NULL; 结构动物*curr=q->first; while(curr!=NULL){ 如果(!strcmp(curr->id,id)){ if(prev==NULL){ q->first=curr->next; } 如果(当前->下一步==NULL){ //在这里我想不出要在这里写些什么 } 否则{ 上一个->下一个=当前->下一个; } 自由(货币->种类); 免费(货币); 返回1; } 否则{ 上一次=当前; 当前=当前->下一步; } } 返回0; } /*分配并初始化一个新队列。 * *返回:指向空队列的指针 */ 我的队列*Q_初始(无效) { My_Queue*q=calloc(1,sizeof(My_Queue)); 返回q; } /*将具有给定动物ID和物种的新成员添加到 *队列结束。 * *返回:如果添加成功,则返回1;如果添加失败,则返回0。附加 失败, *例如,如果动物ID超过6个字符。 */ int Q_enqueue(我的队列*Q,常量字符*id,常量字符*species) { int n=strlen(id); if(n<7){ 结构动物*new=malloc(sizeof(结构动物)); strcpy(新建->标识,标识); 新->物种=malloc(strlen(物种)+1); strcpy(新->种,种); 新建->下一步=空; 如果(q->first==NULL&&q->last==NULL){ q->first=新建; q->last=新; } 否则{ q->last->next=新建; q->last=新; } 返回1; } 否则{ 返回0; } } int main() { /*测试练习。请随意修改此函数*/ 我的队列*q=q_init(); 对于(无符号整数i=0;i嗯,在这一点上

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
部分中有一个节点对应于未找到正确的节点

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;
}
那么在这一点上,

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
部分中有一个节点对应于未找到正确的节点

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;
}

该函数根本不处理匹配是列表中最后一个成员的情况。您应该检查匹配节点是否是最后一个节点(
curr->next==NULL
)并将
q->last
设置为相应地指向上一个节点。还记得将新的最后一项的
next
指针设置为
NULL
,并检查删除的成员是否是列表中唯一的成员,在这种情况下,您不能引用
prev
,必须设置
first
last
指针到
NULL

下面是一个小代码示例:

  p = NULL;
  c = li->first;

  while(c != NULL) {
    if(c->id != id) {
      p = c;
      c = c->next;
      continue;
    }

    /* Only member */
    if(p == NULL && c->next == NULL) {
      li->first = NULL;
      li->last = NULL;
      free(c);
      return;
    }

    /* First member */
    if(p == NULL)
      li->first = c->next;
    else
      p->next = c->next;

    /* Last member */
    if(c->next == NULL) {
      li->last = p;
      p->next = NULL;
    }

    free(c);
    return;
  }

该函数根本不处理匹配是列表中最后一个成员的情况。您应该检查匹配节点是否是最后一个节点(
curr->next==NULL
)并将
q->last
设置为相应地指向上一个节点。还记得将新的最后一项的
next
指针设置为
NULL
,并检查删除的成员是否是列表中唯一的成员,在这种情况下,您可以