从C

时间:2018-03-02 08:52:17

标签: c

这很乱,所以如果你没有时间不要打扰。我试图解释我的代码中的每个函数如何尽可能最好地工作。所以我的问题是我无法工作的函数Q_drop,其他一切(代码的所有其他部分)都运行良好。

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

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

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

所以我的问题是如果匹配节点是最后一个节点,如何将指针移动到前一个节点?我得到的检查错误是“My_Queue->最后一个指针不指向最后一个成员”。

我的代码:

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");

2 个答案:

答案 0 :(得分:2)

嗯,此时

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

答案 1 :(得分:0)

该功能在匹配是列表的最后一个成员的所有情况下都不会处理。您应该检查匹配节点是否是最后一个节点(curr->next == NULL),并将q->last设置为相应地指向前一节点。还要记得将新的最后一项的next指针设置为NULL,并检查删除的成员是否是列表中唯一的成员,在这种情况下,您无法引用{{1并且必须将prevfirst指针设置为last

这里的小代码示例:

NULL