Priority Queue C

空扰寡人 提交于 2019-12-24 23:48:11

问题


I'm trying to create a priority queue using an array of queues, each index of the array being a priority. I tried the following solution,

The queue data type contains an array llist,

Queue *q_create(int size)
{
struct queue *p;
struct q_head *h;
int i;

if ((p = (struct queue *)malloc(sizeof(struct queue))) != NULL) {
    p->size = size;
    for (i = 0; i < PRIODIFF; i++) {
        h = &(p->llist[i]);
        h->head = NULL;
        h->tail = NULL;
    }
}
return p;
}

I'm confused by the line: h = &(p->llist[i]); I was thinking that llist[i] = h. Is this another way of writing it in C? I'm reading it as h = the address of llist[i]. Is this correct?

Thanks


回答1:


I'm reading it as h = the address of llist[i] Is that correct?

Yes.




回答2:


Yes, you should read that as assign the address of p->llist[i] to h. This is not the same as llist[i] = h.

This code is using h as a short-hand to avoid having to type p->llist[i] twice for the two subsequent lines.



来源:https://stackoverflow.com/questions/5779849/priority-queue-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!