Application of Double links in connection between MySQL and C

Source: Internet
Author: User

// C write the data structure used by the MySQL Connection Program <br/> struct harvis_mysql_struct {<br/> MySQL * conn; // The MySQL connection established, global variable <br/> int ID; // sequence number connecting to MySQL, starting from 0 and starting from-1 <br/> unsigned int query_times; // The number of queries executed on a MySQL connection. The initial value is 0 <br/> struct harvis_mysql_struct * Next, * Prev; // point to the next struct <br/> }; <br/> // Max harvis_mysql number <br/> # define maxskillmysql 16 // currently supports establishing 16 dB connections with MySQL at the same time <br/> static struct harvis_mysql_struct * harvis_mysql_entry [m Axskillmysql]; // A array of struct harvis_mysql_struct typed pointer <br/> static struct harvis_mysql_struct limit [maxskillmysql]; <br/> static struct harvis_mysql_struct * Limit = NULL; // unused struct harvis_mysql_struct Single-Chain header pointer <br/> static struct harvis_mysql_struct * harvis_mysql_using_head = NULL; // use the struct harvis_mysql_struct Single-Chain header pointer <br/> static int harvis_mysql_entry _ Id =-1; // serial number. Each time an entry is used, it is incremented by 1. Every time an entry is abandoned, it is subtracted by 1. <br/>/* <br/> * init_harvis_mysql_list-concatenates elements in the array into a linked list. <br/> * void: <br/> */<br/> void init_harvis_mysql_list (void) <br/>{< br/> int I; <br/> for (I = 0; I <maxskillmysql; I ++) <br/> harvis_mysql_entry [I] = & harvis_mysql_entry2 [I]; <br/> for (I = 0; I <maxskillmysql-1; I ++) {// one's next point the next one <br/> harvis_mysql_entry [I]-> next = Harvis _ Mysql_entry [I + 1]; <br/> harvis_mysql_entry [I]-> conn = NULL; <br/> harvis_mysql_entry [I]-> id =-1; <br/> harvis_mysql_entry [I]-> query_times = 0; <br/>}< br/> // initialize the last element of the array <br/> harvis_mysql_entry [I]-> next = NULL; // last element's next pointer <br/> harvis_mysql_entry [I]-> conn = NULL; <br/> harvis_mysql_entry [I]-> id =-1; <br/> harvis_mysql_entry [I]-> query_times = 0; <br/> // initializes the front pointer. <br/> For (I = 1; I <maxskillmysql; I ++) <br/> harvis_mysql_entry [I]-> Prev = harvis_mysql_entry [I-1]; <br/> harvis_mysql_entry [0]-> Prev = NULL; <br/> // assign a value to the header pointer of the idle list. <Br/> harvis_mysql_free_head = harvis_mysql_entry [0]; // free point to the first element of the List <br/>}< br/> // get a struct harvis_mysql_struct type element from the header <br/> struct harvis_mysql_struct * get_harvis_mysql_entry () <br/>{ <br/> struct harvis_mysql_struct * entry; <br/> // checks whether there are idle entries. <br/> If (harvis_mysql_free_head = NULL) <br/> return (struct harvis_mysql_struct *) 0; // if no entry is available, a null pointer is returned. <br/> entry = Harvis_mysql_free_head; <br/> harvis_mysql_free_head = harvis_mysql_free_head-> next; // move free_head to the next entry <br/> If (harvis_mysql_free_head! = NULL) <br/> harvis_mysql_free_head-> Prev = NULL; // The pre-pointer value is null <br/> // entry-> next = NULL; <br/> // Add the entry to the used list <br/> If (harvis_mysql_using_head = NULL) {<br/> entry-> next = NULL; <br/> harvis_mysql_using_head = entry; <br/> harvis_mysql_using_head-> Prev = NULL; // The pre-pointer value is null <br/>} else {<br/> entry-> next = harvis_mysql_using_head; <br/> harvis_mysql_using_head = entry; <br/> harvis_mysql _ Using_head-> Prev = NULL; // The pre-pointer value is null <br/>}< br/> harvis_mysql_entry_id ++; <br/> entry-> conn = NULL; <br/> entry-> id = harvis_mysql_entry_id; <br/> entry-> query_times = 0; <br/> return (entry ); <br/>}< br/> // search by serial number from the applying linked list <br/> struct harvis_mysql_struct * search_harvis_mysql (int id) <br/> {<br/> struct harvis_mysql_struct * entry; <br/> If (ID <0 | ID> maxskillmysql-1) <br/> return (struct H Arvis_mysql_struct *) 0; // the sequence number is invalid <br/> If (harvis_mysql_using_head = NULL) <br/> return (struct harvis_mysql_struct *) 0; // The linked list being applied is empty. <br/> for (Entry = harvis_mysql_using_head; entry! = NULL; entry = entry-> next) {<br/> If (Entry-> id = ID) <br/> return entry; // if no pointer is found, return the pointer of this struct <br/>}< br/> return (struct harvis_mysql_struct *) 0; // No found, returns a null pointer <br/>}< br/> // put a struct harvis_mysql_struct element (cleared) to the header of the List <br/> void free_harvis_mysql_entry (int I) <br/>{< br/> struct harvis_mysql_struct * entry; <br/> entry = search_harvis_mysql (I); <br/> If (entry! = NULL) {<br/> If (Entry-> next! = NULL & Entry-> Prev! = NULL) {<br/> entry-> Prev-> next = entry-> next; // perform the unlink operation <br/> entry-> next-> Prev = entry-> Prev; // remove <br/>} else {<br/> harvis_mysql_using_head = NULL; <br/>}< br/> harvis_mysql_entry_id --; <br/> // clear the entry for future use <br/> entry-> conn = NULL; <br/> entry-> id =-1; <br/> entry-> query_times = 0; <br/> entry-> next = harvis_mysql_free_head; // Add to the header of the idle Object List <br/> entry-> Prev = NULL; // The value of the pre-pointer is null. <Br/> harvis_mysql_free_head = entry; </P> <p >}< br/> // Add an element to the list header <br/> // struct harvis_mysql_struct * add_harvis_mysql_entry (struct harvis_mysql_struct * head, struct harvis_mysql_struct * entry) <br/> // {</P> <p> //} <br/> // obtain the tail element of the list. <br/> struct harvis_mysql_struct * get_harvis_mysql_tail (struct harvis_mysql_struct * head) <br/>{< br/> struct harvis_mysql_struct * entry; <br/> for (Entry = head; Entry-> next! = NULL; entry = entry-> next) <br/>; // empty statement, cannot be omitted <br/> return (entry ); <br/>}< br/> int harvis_mysql_length (struct harvis_mysql_struct * head) <br/>{< br/> int counter = 0; <br/> struct harvis_mysql_struct * entry; <br/> for (Entry = head; entry! = NULL; entry = entry-> next) <br/> counter ++; <br/> return counter; <br/>} 

Related Article

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.