我正在尝试使函数具有优先级队列。我的结构是这样的:
struct node {
char *item;
struct node *next;
};
struct queue {
struct node *start;
struct node *end;
};
struct priority_queue {
struct queue **aoq;
int x;
};
我要实现的功能是:
struct priority_queue *priority_queue_create(int x);
在这里,从结构priority_queue中可以看出,struct queue ** aoq本质上是一个队列数组,这是我想要的。与函数头中一样,int x是数组中的队列数。
我的看法是这样的:
struct priority_queue *priority_queue_create(int x) {
struct priority_queue *pq = malloc(sizeof(struct priority_queue));
pq->x = x;
for (int i = 0; i < x, ++i) {
pq->aoq[i] = malloc(sizeof(struct queue)); ///
}
return pq;
}
我在上面的代码中放置3条注释行的地方是我怀疑我的错误所在。我希望能够执行以下操作:如果x = 3,则应该有一个包含3个队列的数组,并且我想我可以通过执行以下操作来访问它
pq->aoq[0] /// to access the first queue in the array, or
pq->aoq[2] /// to access the third queue in the array
谁能帮我解决我的问题?提前致谢。