c - 从队列中删除所有项目
问题描述
可以说我有以下结构
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);
但它似乎没有工作。任何帮助表示赞赏。
解决方案
您正在寻找一组数组(我认为/希望)。基本上,您想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)
推荐阅读
- postgresql - 如何根据外部表中的日期对表进行分区以保持数据一致性?
- python - 使用 `statsmodels.api.qqplot()` 创建多个子图的最有效方法是什么?
- grails - 如何在错误时不丢失 grails 中的文件上传名称
- selenium - 是否可以从检查元素调试器中的 eval 代码部分抓取数据?
- r - Error in .subset2(x, i, exact = exact) : recursive indexing failed at level 2
- scala - Scala:为什么这个函数中存在类型不匹配?
- python - 在 django 的创建/更新视图中保存图像
- java - 'java.sql.SQLException: 没有合适的驱动程序' [SQLException, Heroku, Postgresql]
- c++ - 从地图中获取价值的最佳实践是什么?
- laravel - Laravel 表行不更新