Ngx_queue_t two-way linked list

Source: Internet
Author: User
: This article mainly introduces the ngx_queue_t two-way linked list. if you are interested in the PHP Tutorial, refer to it. Ngx_queue_t two-way linked list

Structure

typedefstruct ngx_queue_s ngx_queue_t;typedefstruct ngx_queue_s {    ngx_queue_t *prev;    ngx_queue_t *next;};

The structure of the entire linked list is: there is an empty header, which is used as the start and sentry of the linked list (used for the traversal), and then add nodes to the subsequent position of this header using the following method.

Container-provided method

Method name Parameter description Meaning
Ngx_queue_init (h) H is the pointer of ngx_queue_t. Linked list container initialization, empty linked list
Ngx_queue_empty (h) Same as above Check whether the linked list is empty
Nxg_queue_insert_head (h, x) H. same as above. x indicates the ngx_queue_t pointer to be inserted. Header insertion method
Nxg_queue_insert_head (h, x) Same as above Tail insertion method
Ngx_queue_head (h) Same as above Return header pointer
Ngx_queue_tail (h) Same as above Returns the tail pointer.
Ngx_queue_sentinel (h) Same as above Returns the struct pointer.
Ngx_queue_remove (x) Same as above Remove x element
Ngx_queue_split (h, q, n) Same as above The linked list is divided into two parts: h-> q (excluding q) and q-> tail. the pointers in the latter part are stored on n.
Ngx_queue_add (h, n) Both h and n are two-way linked list container pointers. Merge the linked list and connect n after h
Ngx_queue_middle (h) Same as above Returns the pointer to the N/2 + 1 element.
Ngx_queue_sort (h, compfunc) Cmpfunc is an element comparison method. Use the ** insert sort ** cpmfunc prototype: ngx_int_t (* cpmfunc) (const ngx_queue_t * a, const ngx_queue_t * B)

Method of element in two-way linked list

Method name Parameter description Meaning
Ngx_queue_next (q) Q is the pointer of a struct variable ngx_queue_t in the linked list. Returns the next element.
Ngx_queue_prev (q) Same as above Returns the previous element.
Ngx_queue_data (q, type, member) Q. same as above, type is the struct type, and member is the name of ngx_queue_t in this struct. Returns the first address of the struct variable where the q variable is located.
Ngx_queue_insert (q, x) Q and x are the ngx_queue_t member pointers in a struct variable. Insert the struct variable of x to the struct variable of q.

Test sort

# Include
  
   
# Include ". /ngx_queue.h "// The struct typedefstruct _ TestNode {u_char * str; ngx_queue_t qEle; // contains ngx_queue_t to form a bidirectional linked list int num;} TestNode; // Set the comparison function ngx_int_t compTestNode (const ngx_queue_t * a, const ngx_queue_t * B) {TestNode * aNode = ngx_queue_data (a, TestNode, qEle ); testNode * bNode = ngx_queue_data (B, TestNode, qEle); return aNode-> num> bNode-> num;} // Print the content of the linked list in sequence void printQueue (ngx_queue_t * head) {ngx_queue_t * PNode = NULL; for (pNode = ngx_queue_head (head); \ pNode! = Ngx_queue_sentinel (head); \ pNode = ngx_queue_next (pNode) {TestNode * node = ngx_queue_data (pNode, TestNode, qEle); printf ("% d ", node-> num);} printf ("\ n");} int main () {ngx_queue_t queueContainer; // Create a ngx_queue_t variable ngx_queue_init (& queueContainer ); // Initialize the variableint I = 0; TestNode node [5]; for (; I <5; ++ I) node [I]. num = I; // consider the inserted sequence ngx_queue_insert_tail (& queueContainer, & node [0]. qEle); ngx_queue_insert_head (& queueContainer, & node [1]. qEle); ngx_queue_insert_tail (& queueContainer, & node [2]. qEle); ngx_queue_insert_after (& queueContainer, & node [3]. qEle); ngx_queue_insert_tail (& queueContainer, & node [4]. qEle); // Pirnt queue printQueue (& queueContainer); // Sort events (& queueContainer, compTestNode); // Print queue after sort printQueue (& queueContainer); return0 ;}
  

Note:

For the "ngx_queue.h" header file used, I copied it from the Nginx source code and modified it. it's quite simple and it won't work anymore.

Copyright: Pain is just in your mind.

The ngx_queue_t two-way linked list is introduced above, including some content, and it is helpful for friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.