int main() { ngx_qTest_t arr[5]; ngx_queue_t head; ngx_queue_init(&head); int i=0; for( ; i<5; ++i)//初始化 队列 { arr[i].key = i; sprintf(arr[i].name, "\"My key is:%d.\"", arr[i].key); if(i%2) { ngx_queue_insert_head(&head, &arr[i].link); } else { ngx_queue_insert_tail(&head, &arr[i].link); } } printf("*******************************\n"); printf("Before sort:\n"); print_queue(&head); ngx_queue_sort(&head, cmp); printf("*******************************\n"); printf("After sort:\n"); print_queue(&head); ngx_queue_t * midq = ngx_queue_middle(&head); ngx_qTest_t *mid_data = ngx_queue_data(midq, ngx_qTest_t, link); printf("*******************************\n"); // 求取中间节点 printf("%The middle key is %d.\n", mid_data->key); return 0; }
static ngx_http_location_tree_node_t * ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations, size_t prefix) { size_t len; ngx_queue_t *q, tail; ngx_http_location_queue_t *lq; ngx_http_location_tree_node_t *node; q = ngx_queue_middle(locations); lq = (ngx_http_location_queue_t *) q; len = lq->name->len - prefix; node = ngx_palloc(cf->pool, offsetof(ngx_http_location_tree_node_t, name) + len); if (node == NULL) { return NULL; } node->left = NULL; node->right = NULL; node->tree = NULL; node->exact = lq->exact; node->inclusive = lq->inclusive; node->auto_redirect = (u_char) ((lq->exact && lq->exact->auto_redirect) || (lq->inclusive && lq->inclusive->auto_redirect)); node->len = (u_char) len; ngx_memcpy(node->name, &lq->name->data[prefix], len); ngx_queue_split(locations, q, &tail); if (ngx_queue_empty(locations)) { /* * ngx_queue_split() insures that if left part is empty, * then right one is empty too */ goto inclusive; } node->left = ngx_http_create_locations_tree(cf, locations, prefix); if (node->left == NULL) { return NULL; } ngx_queue_remove(q); if (ngx_queue_empty(&tail)) { goto inclusive; } node->right = ngx_http_create_locations_tree(cf, &tail, prefix); if (node->right == NULL) { return NULL; } inclusive: if (ngx_queue_empty(&lq->list)) { return node; } node->tree = ngx_http_create_locations_tree(cf, &lq->list, prefix + len); if (node->tree == NULL) { return NULL; } return node; }