#include<iostream>
#include<ngx_queue.h>
using namespace std;
/* This node struc must contain a ngx_queue_t member */
typedef struct {
u_char *str;
ngx_queue_t qEle;
int key;
} Node;
/* cmpfunc for asc sort */
ngx_int_t nodeCmp(const ngx_queue_t *a, const ngx_queue_t *b)
{
Node *nda = ngx_queue_data(a, Node, qEle);
Node *ndb = ngx_queue_data(b, Node, qEle);
return nda->key > ndb->key;
}
/* Traverse the queue*/
void traverseQueue(ngx_queue_t &queueContainer)
{
ngx_queue_t *q;
for( q = ngx_queue_head(&queueContainer);/* q = ngx_queue_last(&queueContainer); */
q != ngx_queue_sentinel(&queueContainer);
q = ngx_queue_next(q) /* q = ngx_queue_prev(q) */ )
{
Node *node = ngx_queue_data(q, Node, qEle);
cout<<node->key<<' ';
}
cout<<endl;
}
int main(void)
{
ngx_queue_t queueContainer;
ngx_queue_init(&queueContainer);
Node nodes[5];
for(int i = 0; i < 5; ++i)
{
nodes[i].key = i;
}
/* Insert nodes to the queue*/
ngx_queue_insert_head(&queueContainer, &nodes[0].qEle);
ngx_queue_insert_tail(&queueContainer, &nodes[1].qEle);
ngx_queue_insert_after(&queueContainer, &nodes[2].qEle);
ngx_queue_insert_after(&nodes[2].qEle, &nodes[3].qEle);
ngx_queue_insert_tail(&queueContainer, &nodes[4].qEle);
traverseQueue(queueContainer);
/* output: 2 3 0 1 4 */
ngx_queue_sort(&queueContainer, nodeCmp);
traverseQueue(queueContainer);
/* output: 0 1 2 3 4 */
return 1;
}
nginx双向队列ngx_queue_t使用例子
最新推荐文章于 2021-07-10 14:06:38 发布