雙鏈在C與MySql串連時的應用

來源:互聯網
上載者:User

//C寫MySQL串連程式所用的資料結構<br />struct harvis_mysql_struct {<br /> MYSQL *conn; //建立的MySql串連,全域變數<br /> int id; //串連MySql的順序號,從0開始,初始值為-1<br /> unsigned int query_times; //一個MySql串連上執行的查詢次數,初始值為0<br /> struct harvis_mysql_struct *next, *prev;//指向下一個結構體<br />};<br />//max harvis_mysql number<br />#define MAXSKILLMYSQL 16 //現在支援與MySql同時建立16個db的串連<br />static struct harvis_mysql_struct *harvis_mysql_entry[MAXSKILLMYSQL]; //a array of struct harvis_mysql_struct typed pointer<br />static struct harvis_mysql_struct harvis_mysql_entry2[MAXSKILLMYSQL];<br />static struct harvis_mysql_struct *harvis_mysql_free_head = NULL; //未使用的struct harvis_mysql_struct單鏈的頭指標<br />static struct harvis_mysql_struct *harvis_mysql_using_head = NULL; //使用著的struct harvis_mysql_struct單鏈的頭指標<br />static int harvis_mysql_entry_id = -1;//序號,每使用一個entry它就加1,每放棄一個entry它就減1.<br />/*<br /> * init_harvis_mysql_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 /> //對數組的最後一個元素進行初始化<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 /> //對前置指標進行初始化<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 /> //對空閑列表的頭指標進行賦值。<br /> harvis_mysql_free_head = harvis_mysql_entry[0];//free point to the first element of the list<br />}<br />//從表頭取一個struct harvis_mysql_struct類型的元素<br />struct harvis_mysql_struct *get_harvis_mysql_entry()<br />{<br /> struct harvis_mysql_struct *entry;<br /> //檢測是否還有閒置entry<br /> if (harvis_mysql_free_head == NULL)<br /> return (struct harvis_mysql_struct *)0;//沒有可用的entry了,直接返回null 指標<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;//前置指標賦值為NULL<br /> //entry->next = NULL;<br /> //將entry添加到已用的列表中<br /> if (harvis_mysql_using_head == NULL) {<br /> entry->next = NULL;<br /> harvis_mysql_using_head = entry;<br /> harvis_mysql_using_head->prev = NULL;//前置指標賦值為NULL<br /> } else {<br /> entry->next = harvis_mysql_using_head;<br /> harvis_mysql_using_head = entry;<br /> harvis_mysql_using_head->prev = NULL;//前置指標賦值為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 />//從正在應用的鏈表中按序號尋找<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 harvis_mysql_struct *)0;//順序號非法<br /> if (harvis_mysql_using_head == NULL)<br /> return (struct harvis_mysql_struct *)0;//正在應用的鏈表為空白<br /> for (entry = harvis_mysql_using_head; entry != NULL; entry = entry->next) {<br /> if (entry->id == id)<br /> return entry;//找到了則返回此結構體的指標<br /> }<br /> return (struct harvis_mysql_struct *)0;//沒有尋找的到,則返回null 指標<br />}<br />//將一個struct harvis_mysql_struct類型的元素(清空後)放到列表的頭部<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; //脫鏈操作<br /> entry->next->prev = entry->prev; //從正在使用的鏈表中脫去<br /> } else {<br /> harvis_mysql_using_head = NULL;<br /> }<br /> harvis_mysql_entry_id--;<br /> //對entry進行清空,以備下次使用<br /> entry->conn = NULL;<br /> entry->id = -1;<br /> entry->query_times = 0;<br /> entry->next = harvis_mysql_free_head;//添加到閒置實體列表頭部<br /> entry->prev = NULL;//前置指標賦值為NULL<br /> harvis_mysql_free_head = entry;</p><p> }<br />}<br />//往列表的頭部添加一個元素<br />//struct harvis_mysql_struct *add_harvis_mysql_entry(struct harvis_mysql_struct *head, struct harvis_mysql_struct *entry)<br />//{</p><p>//}<br />//擷取列表的尾部元素<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 /> ;//空語句,不能夠省略掉<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 />} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.