首页 > 解决方案 > 从队列中删除所有项目

问题描述

可以说我有以下结构

typedef struct node {
    unsigned long data;
    struct node* next;
} node;

typedef struct queue {
    node* head;
    node* tail;
    int size;
    int capacity;
} queue;

queue* buildar()
{
    queue* arr = malloc(num * sizeof(queue));
    int i;
    for (i = 0; i < num; i++)
    {
        queue* r = malloc(sizeof(queue));
        r->head = NULL;
        r->tail = NULL;
        r->size = 0;
        r->capacity = assoc;
        arr[i] = *r;
    }
    return arr;
}

如何释放队列中的所有项目?我已经尝试过这样做

    for (size_t i = 0; i < numberofsets; i++)
    {
        node* temp = arr[i].head;
        arr[i].head = arr[i].head->next;
        free(temp);
    }
    free(arr);

但它似乎没有工作。任何帮助表示赞赏。

标签: cdata-structuresqueue

解决方案


您正在寻找一组数组(我认为/希望)。基本上,您想n从一个“主”数组创建所有可索引的队列(这是一个词吗?)。

所以你是这样看的:

idx
of
big
arry
_________________________________  
|000|   ----->  [ one big queue ]  
|001|   ----->  [ one big queue ]  
|002|   ----->  [ one big queue ]  
|003|   ----->  [ one big queue ]  
|004|   ----->  [ one big queue ]  
|...|   ----->  .................   
|nth|   ----->  [ one big queue ]  
_________________________________  
#include <stdlib.h>


typedef struct node
{
    unsigned long data;
    struct node*  next;
}
node;

typedef struct queue
{
    int   size;
    int   capacity;
    node* head;
    node* tail;
    
}
queue;


queue** build_queue(int num  ,
                    int assoc
                   )
{
    //here you create an array of arrays
    //result is an array of pointers, each pointer going to a separate array 
    queue** result = malloc(num * sizeof(queue*));
    
    int i;
    for (i = 0; i < num; i++)
    {
        queue* q    = malloc(sizeof(queue));  //memory for actual queue
        result[i]   = q;

        q->head     = NULL;
        q->tail     = NULL;
        q->size     = 0;
        q->capacity = assoc;
    }

    return result;
}



void free_queues(int     num_queues,
                 queue** qs
                )
{
    int i;
    for (i = 0; i < num_queues; i++)
    {
       /*
        * NOTE: you must use a while loop here to FIRST free the linked list qs[i]
        * node* head = qs[i]->head;
        * while(head != qs[i]->tail)
        * {
        *     node* temp = head->next;
              free(head);
              head = temp;
        * }
        * free(qs[i]->tail);
        */
       free(qs[i]);  //frees the actual queue struct   
    }
    free(qs);        //frees the array of pointers to queues
}



int main(void)
{
   int num   = 5;
   int assoc = 4;
 
   queue** qs = build_queue(num, assoc);
   free_queues(num, qs);

   return 1;
}

最后,用于valgrind检查是否有泄漏。对于上述解决方案:

$ gcc please_work.c
$ valgrind ./a.out 
==5974== Memcheck, a memory error detector
==5974== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5974== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5974== Command: ./a.out
==5974== 
==5974== 
==5974== HEAP SUMMARY:
==5974==     in use at exit: 0 bytes in 0 blocks
==5974==   total heap usage: 6 allocs, 6 frees, 160 bytes allocated
==5974== 
==5974== All heap blocks were freed -- no leaks are possible
==5974== 
==5974== For lists of detected and suppressed errors, rerun with: -s
==5974== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


推荐阅读