gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:23 25 4
gpt4 key购买 nike

<分区>

这很乱,所以如果您没有时间,请不要打扰。我已尽力解释我的代码中的每个函数如何发挥最佳作用。所以我的问题是函数 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");

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com