zoukankan      html  css  js  c++  java
  • ngx_queue的理解

    这儿http://code.google.com/p/nginxsrp/wiki/NginxCodeReview,针对nginx的一些细节进行了讲解,看了受益良多.看到ngx_queue的时候,发现其实是一个简单的双向链表,居然看来看去理不清这个链表是怎么运作的,只好画图,然后发现,原来这个双向列表的prev实际表示的是next,这样一来理解起来就容易多了.

    以下的代码为转载:

    下面是一个queue操作的例子

    #include<stdio.h> 
    #include"ngx_config.h" 
    #include"ngx_conf_file.h" 
    #include"nginx.h" 
    #include"ngx_core.h" 
    #include"ngx_string.h" 
    #include"ngx_palloc.h" 
    #include"ngx_queue.h" 
     
    volatile ngx_cycle_t  *ngx_cycle; 
    void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,constchar*fmt,...){} 
    // 用雅虎的成员列表作为一个简单的例子 
    typedef struct yahoo_s 
    {     
        ngx_queue_t   queue; 
    } yahoo_t; 
    typedef struct yahoo_guy_s 
    {     
        ngx_uint_t    id;  
        u_char*       name;  
        ngx_queue_t   queue;
    } yahoo_guy_t;  
    
    // 排序使用的比较函数, 按照id的大小排序,id大放到到前面 
    ngx_int_t yahoo_no_cmp(const ngx_queue_t* p, const ngx_queue_t* n) 
    {     
        yahoo_guy_t *pre, *next;     
        pre  = (yahoo_guy_t*) ngx_queue_data(p, yahoo_guy_t, queue);   
        next = (yahoo_guy_t*) ngx_queue_data(n, yahoo_guy_t, queue); 
        return ((pre->id > next->id) ? 1:0); 
    }  
    
    int main() 
    {      
        int i; 
        ngx_pool_t*     pool;   
        yahoo_guy_t*    guy;    
        ngx_queue_t*    q;   
        yahoo_t*        yahoo;    
       
        // 构建队列     
        const ngx_str_t   names[] = 
        {  ngx_string("rainx"),ngx_string("xiaozhe"), ngx_string("zhoujian")   } ;     
        const int ids[]  = 
        { 4611,  8322, 6111 };      
        
        pool = ngx_create_pool(1024*10, NULL); //初始化内存池   
        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;
    }

    运行结果为

    rainx@rainx-laptop:~/land/nginxsrp/src/demo/basic_types$ ./queue_op  
    No.4611 guy in yahoo is rainx  
    No.8322 guy in yahoo is xiaozhe  
    No.6111 guy in yahoo is zhoujian  
    sorting
    .... 
    No.8322 guy in yahoo is xiaozhe  
    No.6111 guy in yahoo is zhoujian  
    No.4611 guy in yahoo is rainx

    插入的示意图如下图所示:

  • 相关阅读:
    redis学习笔记(三)——redis的命令大全总结
    redis学习笔记(二)——java中jedis的简单使用
    redis学习笔记(一)——windows下redis的安装与配置
    快速排序
    SpringMvc实现批量删除,使用post传值一直报404错误
    Bootstrap-table 显示行号
    选择排序
    冒泡排序
    org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class
    Jquery Validate动态添加和删除校验规则
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/2735406.html
Copyright © 2011-2022 走看看