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; }
int main(int argc, char const *argv[]) { ngx_queue_t queueContainer; ngx_queue_init(&queueContainer); int i = 0; my_queue node[5]; for(;i < 5;++i){ node[i].num = i; } ngx_queue_insert_tail(&queueContainer,&node[0].que); ngx_queue_insert_head(&queueContainer,&node[1].que); ngx_queue_insert_tail(&queueContainer,&node[2].que); ngx_queue_insert_after(&queueContainer,&node[3].que); ngx_queue_insert_tail(&queueContainer,&node[4].que); ngx_queue_sort(&queueContainer,compTestNode); ngx_queue_t *q; for(q = ngx_queue_head(&queueContainer);q != ngx_queue_sentinel(&queueContainer) ;q = ngx_queue_next(q)){ my_queue* eleNode = ngx_queue_data(q,my_queue,que); printf("%d\n",eleNode -> num ); } return 0; }
void ngx_queue_t_test() { int i = 0; ngx_pool_t* pool; ngx_queue_t* myque; my_point_queue_t* point; my_point_t points[POINTS_LEN] = { {10, 1}, {20, 9}, {9, 9}, {90, 80}, {5, 3}, {50, 20} }; printf("----------------------------------------\n"); printf("the size of ngx_queue_t: \n"); printf("----------------------------------------\n"); printf("%lu\n\n", sizeof(ngx_queue_t)); printf("----------------------------------------\n"); printf("create a new pool: \n"); printf("----------------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("----------------------------------------\n"); printf("alloc a queue head and nodes: \n"); printf("----------------------------------------\n"); myque = ngx_palloc(pool, sizeof(ngx_queue_t)); ngx_queue_init(myque); for(i = 0; i < POINTS_LEN; ++i) { point = (my_point_queue_t*)ngx_palloc(pool, sizeof(my_point_queue_t)); point->point.x = points[i].x; point->point.y = points[i].y; ngx_queue_init(&point->queue); ngx_queue_insert_head(myque, &point->queue); } dump_queue_from_tail(myque); printf("\n"); printf("----------------------------------------\n"); printf("sort the queue: \n"); printf("----------------------------------------\n"); ngx_queue_sort(myque, my_point_cmp); dump_queue_from_head(myque); printf("\n"); printf("----------------------------------------\n"); printf("the pool at the end: \n"); printf("----------------------------------------\n"); dump_pool(pool); ngx_destroy_pool(pool); }
int main(int argc, char *argv[]) { ngx_int_t n; ngx_log_t * log; ngx_queue_t queue_container; ngx_queue_t *q; test_node node[5]; test_node *ele_node; ngx_pagesize = getpagesize(); n = ngx_strerror_init(); if (n == NGX_ERROR){ return NGX_ERROR; } ngx_time_init(); log = ngx_log_init((u_char *)"./"); ngx_use_stderr = 0; if (log == NULL){ ngx_log_stderr(NGX_ERROR,(const char*)"can not init log "); return NGX_ERROR; } ngx_queue_init(&queue_container); for (n = 0;n < 5;n++){ node[n].num = n; } ngx_queue_insert_tail(&queue_container,&node[0].queue); ngx_queue_insert_head(&queue_container,&node[1].queue); ngx_queue_insert_after(&queue_container,&node[2].queue); ngx_queue_insert_head(&queue_container,&node[3].queue); ngx_queue_insert_tail(&queue_container,&node[4].queue); for (q = ngx_queue_head(&queue_container); q != ngx_queue_sentinel(&queue_container); q = ngx_queue_next(q)){ ele_node = ngx_queue_data(q,test_node,queue); printf("%d\n",ele_node->num); } ngx_queue_sort(&queue_container,cmp_test_node); for (q = ngx_queue_head(&queue_container); q != ngx_queue_sentinel(&queue_container); q = ngx_queue_next(q)){ ele_node = ngx_queue_data(q,test_node,queue); printf("%d\n",ele_node->num); } return NGX_OK; }
int main() { ngx_pool_t* pool; yahoo_guy_t* guy; ngx_queue_t* q; yahoo_t* yahoo; pool = ngx_create_pool(1024*10, NULL); //初始化内存池 int i; // 构建队列 const ngx_str_t names[] = { ngx_string("rainx"), ngx_string("xiaozhe"), ngx_string("zhoujian") } ; const int ids[] = {4611, 8322, 6111}; yahoo = ngx_palloc(pool, sizeof(yahoo_t)); ngx_queue_init(&yahoo->queue); //初始化queue for(i = 0; i < 3; i++) { guy = (yahoo_guy_t*) ngx_palloc(pool, sizeof(yahoo_guy_t)); guy->id = ids[i]; //guy->name = (char*) ngx_palloc(pool, (size_t) (strlen(names[i]) + 1) ); guy->name = (u_char*) ngx_pstrdup(pool, (ngx_str_t*) &(names[i]) ); ngx_queue_init(&guy->queue); // 从头部进入队列 ngx_queue_insert_head(&yahoo->queue, &guy->queue); } // 从尾部遍历输出 for(q = ngx_queue_last(&yahoo->queue); q != ngx_queue_sentinel(&yahoo->queue); q = ngx_queue_prev(q) ) { guy = ngx_queue_data(q, yahoo_guy_t, queue); printf("No. %d guy in yahoo is %s \n", guy->id, guy->name); } // 排序从头部输出 ngx_queue_sort(&yahoo->queue, yahoo_no_cmp); printf("sorting....\n"); for(q = ngx_queue_prev(&yahoo->queue); q != ngx_queue_sentinel(&yahoo->queue); q = ngx_queue_last(q) ) { guy = ngx_queue_data(q, yahoo_guy_t, queue); printf("No. %d guy in yahoo is %s \n", guy->id, guy->name); } ngx_destroy_pool(pool); return 0; }
int main() { ngx_pool_t *pool; ngx_queue_t *myque; my_point_queue_t *point; my_point_t points[Max_Num] = { {10, 1}, {20, 9}, {9, 9}, {90, 80}, {5, 3}, {50, 20} }; int i; printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("--------------------------------\n"); printf("alloc a queue head and nodes :\n"); printf("--------------------------------\n"); myque =ngx_palloc(pool, sizeof(ngx_queue_t)); //alloc a queue head ngx_queue_init(myque); //init the queue //insert some points into the queue for (i = 0; i < Max_Num; i++) { point = (my_point_queue_t*)ngx_palloc(pool, sizeof(my_point_queue_t)); point->point.x = points[i].x; point->point.y = points[i].y; ngx_queue_init(&point->queue); //insert this point into the points queue ngx_queue_insert_head(myque, &point->queue); } dump_queue_from_tail(myque); printf("\n"); printf("--------------------------------\n"); printf("sort the queue:\n"); printf("--------------------------------\n"); ngx_queue_sort(myque, my_point_cmp); dump_queue_from_head(myque); printf("\n"); printf("--------------------------------\n"); printf("the pool at the end:\n"); printf("--------------------------------\n"); dump_pool(pool); ngx_destroy_pool(pool); return 0; }
static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, ngx_http_core_loc_conf_t *pclcf) { ngx_uint_t n; ngx_queue_t *q, *locations, *named, tail; ngx_http_core_loc_conf_t *clcf; ngx_http_location_queue_t *lq; ngx_http_core_loc_conf_t **clcfp; #if (NGX_PCRE) ngx_uint_t r; ngx_queue_t *regex; #endif locations = pclcf->locations; if (locations == NULL) { return NGX_OK; } ngx_queue_sort(locations, ngx_http_cmp_locations); named = NULL; n = 0; #if (NGX_PCRE) regex = NULL; r = 0; #endif for (q = ngx_queue_head(locations); q != ngx_queue_sentinel(locations); q = ngx_queue_next(q)) { lq = (ngx_http_location_queue_t *) q; clcf = lq->exact ? lq->exact : lq->inclusive; if (ngx_http_init_locations(cf, NULL, clcf) != NGX_OK) { return NGX_ERROR; } #if (NGX_PCRE) if (clcf->regex) { r++; if (regex == NULL) { regex = q; } continue; } #endif if (clcf->named) { n++; if (named == NULL) { named = q; } continue; } if (clcf->noname) { break; } } if (q != ngx_queue_sentinel(locations)) { ngx_queue_split(locations, q, &tail); } if (named) { clcfp = ngx_palloc(cf->pool, (n + 1) * sizeof(ngx_http_core_loc_conf_t **)); if (clcfp == NULL) { return NGX_ERROR; } cscf->named_locations = clcfp; for (q = named; q != ngx_queue_sentinel(locations); q = ngx_queue_next(q)) { lq = (ngx_http_location_queue_t *) q; *(clcfp++) = lq->exact; } *clcfp = NULL; ngx_queue_split(locations, named, &tail); } #if (NGX_PCRE) if (regex) { clcfp = ngx_palloc(cf->pool, (r + 1) * sizeof(ngx_http_core_loc_conf_t **)); if (clcfp == NULL) { return NGX_ERROR; } pclcf->regex_locations = clcfp; for (q = regex; q != ngx_queue_sentinel(locations); q = ngx_queue_next(q)) { lq = (ngx_http_location_queue_t *) q; *(clcfp++) = lq->exact; } *clcfp = NULL; ngx_queue_split(locations, regex, &tail); } #endif return NGX_OK; }