一個通用純C隊列的實現

https://blog.csdn.net/kxcfzyk/article/details/31728179

隊列并不是很復雜的數據結構,但是非常實用,這里實現一個隊列是因為在我的另一篇博客非常精簡的Linux線程池實現中要用到。

?

隊列API定義如下:

?

 
  1. //queue.h

  2. ?
  3. #ifndef QUEUE_H_INCLUDED

  4. #define QUEUE_H_INCLUDED

  5. ?
  6. typedef struct queue *queue_t;

  7. ?
  8. queue_t queue_create();

  9. ?
  10. int queue_isempty(queue_t q);

  11. ?
  12. void* queue_enqueue(queue_t q, unsigned int bytes);

  13. ?
  14. void* queue_dequeue(queue_t q);

  15. ?
  16. void queue_destroy(queue_t q);

  17. ?
  18. #endif //QUEUE_H_INCLUDED

隊列API提供的功能有:創建隊列,判斷隊列是否為空,入隊,出隊,銷毀隊列。這個隊列是通用的,不針對特定數據類型,它里面存儲的元素是void*類型的指針。注意這個隊列跟普通隊列的入隊操作有所不同。普通隊列的入隊操作通常如下:

?

 
  1. struct type *p;

  2. p = malloc(sizeof(struct type));

  3. p->a = ...;

  4. p->b = ...;

  5. p->c = ...;

  6. ...

  7. queue_enqueue(q, p);

而這里的入隊操作簡化了流程:

?

 
  1. struct type *p;

  2. p=queue_enqueue(q, sizeof(struct type));

  3. p->a = ...;

  4. p->b = ...;

  5. p->c = ...;

  6. ...

另外雖然隊列元素(指針)所指向的內存空間是在入隊操作時由隊列分配的,但是隊列元素出隊以后,隊列并不負責元素所指向內存空間的釋放,隊列使用者應該自己手動釋放內存。

?

隊列的實現如下:

?

 
  1. //queue.c

  2. ?
  3. #include "queue.h"

  4. #include <stdlib.h>

  5. ?
  6. struct node {

  7. void *element;

  8. struct node *next;

  9. };

  10. ?
  11. struct queue {

  12. struct node front;

  13. struct node *tail;

  14. };

  15. ?
  16. queue_t queue_create() {

  17. queue_t q;

  18. q=(queue_t)malloc(sizeof(struct queue));

  19. q->front.element=NULL;

  20. q->front.next=NULL;

  21. q->tail=&q->front;

  22. return q;

  23. }

  24. ?
  25. int queue_isempty(queue_t q) {

  26. return &q->front==q->tail;

  27. }

  28. ?
  29. void* queue_enqueue(queue_t q, unsigned int bytes) {

  30. q->tail->next=(struct node*)malloc(sizeof(struct node));

  31. q->tail->next->element=malloc(bytes);

  32. q->tail->next->next=NULL;

  33. q->tail=q->tail->next;

  34. return q->tail->element;

  35. }

  36. ?
  37. void* queue_dequeue(queue_t q) {

  38. struct node *tmp=q->front.next;

  39. void *element;

  40. if(tmp==NULL) {

  41. return NULL;

  42. }

  43. element=tmp->element;

  44. q->front.next=tmp->next;

  45. free(tmp);

  46. if(q->front.next==NULL) {

  47. q->tail=&q->front;

  48. }

  49. return element;

  50. }

  51. ?
  52. void queue_destroy(queue_t q) {

  53. struct node *tmp, *p=q->front.next;

  54. while(p!=NULL) {

  55. tmp=p;

  56. p=p->next;

  57. free(tmp);

  58. }

  59. free(q); // 感謝@Toudsour指正

  60. }

應用程序使用隊列時只需要包含queue.h頭文件,并在編譯時將queue.c一起編譯就行了。因為隊列的聲明和實現是嚴格分離的,包含queue.h的應用程序無法也不應該通過隊列指針直接訪問隊列結構體的成員。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383648.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383648.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383648.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Dijkstra算法介紹+正確性證明+性能分析

算法介紹 源點s,數組d[u]表示s到u的最短距離&#xff0c;空集S&#xff0c;點集Q初始化&#xff1a;將源點s從點集中去掉&#xff0c;加入S&#xff0c;d[s]0&#xff0c;?v∈Q,d[v]w[s][v]\forall v\in Q ,d[v]w[s][v]?v∈Q,d[v]w[s][v]將Q中d[v]最小的點去掉加入S,并對u∈…

Linux C 實現一個簡單的線程池

https://www.cnblogs.com/GyForever1004/p/9185240.html 線程池的定義 線程池是一種多線程處理形式&#xff0c;處理過程中將任務添加到隊列&#xff0c;然后在創建線程后自動啟動這些任務。線程池線程都是后臺線程。每個線程都使用默認的堆棧大小&#xff0c;以默認的優先級…

斐波那契數列求解+尾遞歸

1.普通遞歸 這里觀察f[4]的遞歸樹代替f[10]的遞歸樹&#xff08;后者比較大&#xff0c;畫不下&#xff09;。 使用遞歸求解的時候復雜度為T(n)T(n?1)T(n?2)T(n)T(n-1)T(n-2)T(n)T(n?1)T(n?2)&#xff0c;觀察遞歸樹&#xff0c;發現降速最快的是最右邊每次減2&#xff0c…

循環服務器,并發服務器模型以及I/O多路轉接模型

https://blog.csdn.net/xinianbuxiu/article/details/53455784 一、基于TCP/IP協議的基本循環服務器 tcp_server.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #incl…

c++繼承父類的子類,如何調用父類的同名函數?

https://blog.csdn.net/qq_26399665/article/details/52080215 子類調用父類的同名函數&#xff1a; 子類和父類返回值參數相同&#xff0c;函數名相同&#xff0c;有virtual關鍵字&#xff0c;則由對象的類型決定調用哪個函數。 子類和父類只要函數名相同&#xff0c;沒有vi…

LCS最長公共子串

問題介紹 LCS問題(longest common subsequence problem)指的是求解兩個字符串最長公共子序列問題。這里的子序列是可以不連續的。LCS問題廣泛地出現在計算生物學中&#xff08;DNA序列、系統生成樹等等&#xff09;。這里介紹如何解決LCS問題&#xff0c;以及算法的正確性證明…

將字符串中的空格用%20替換

如果不需要原地操作&#xff0c;則一遍遍歷&#xff0c;將非空串復制&#xff0c;遇到空格加上%20&#xff0c;如果需要原地操作&#xff0c;首先進行遍歷出空格的個數x,然后擴容2x,從后往前遍歷實現。如果非空格字符串比空格字符串多的多的時候而且字符串非常長的時候使用原地…

12步輕松搞定python裝飾器

http://python.jobbole.com/81683/ 呵呵&#xff01;作為一名教python的老師&#xff0c;我發現學生們基本上一開始很難搞定python的裝飾器&#xff0c;也許因為裝飾器確實很難懂。搞定裝飾器需要你了解一些函數式編程的概念&#xff0c;當然還有理解在python中定義和調用函數…

操作系統【六】虛擬內存

傳統存儲管理方式的不足 一次性&#xff1a;作業必須一次性全部裝入內存后才能開始運行。這會造成&#xff1a;當作也很大時不能全部裝入內存&#xff1b;當大量作業要求運行時&#xff0c;由于內存無法容納所有作業&#xff0c;因此只有少量作業能夠運行&#xff0c;導致多道…

python裝飾器詳解

https://blog.csdn.net/xiangxianghehe/article/details/77170585 你會Python嘛&#xff1f; 我會&#xff01; 那你給我講下Python裝飾器吧&#xff01; Python裝飾器啊&#xff1f;我沒用過哎 以上是我一個哥們面試時候發生的真實對白。 ———————————————-分…

SQL Server【一】簡介和基本概念和命令

數據結構和數據庫的區別 數據庫是應用軟件級別研究數據的存儲和操作&#xff08;主要針對磁盤上的數據&#xff09; 數據結構是在系統軟件級別研究數據的存儲和操作&#xff08;主要是針對內存中的數據&#xff09; 對硬盤數操作是數據庫的強項&#xff0c;是數據庫研究的核心…

SQL Server【二】單表查詢

查詢 計算列 select * from emp; -- *通配符&#xff0c;表示所有的字段 -- from emp 從emp表查詢select empno, ename from emp; select ename as "員工姓名", sal*12 as "年薪" from emp;-- as可以省略&#xff0c;用于設置字段名 -- 注意用雙引號將字…

SQL Server【三】連接查詢

將兩個表或者兩個以上的表以一定的連接條件連接起來&#xff0c;從中檢索出滿足條件的數據。 內連接 使用inner join&#xff0c;inner可以省略 -- 查詢員工的姓名和部門名稱 select "E".ename as "員工姓名", "D".dname as "部門名稱&q…

Linux下網絡socket編程——實現服務器(select)與多個客戶端通信

一、關于socket通信 服務器端工作流程&#xff1a; 調用 socket() 函數創建套接字 用 bind() 函數將創建的套接字與服務端IP地址綁定調用listen()函數監聽socket() 函數創建的套接字&#xff0c;等待客戶端連接 當客戶端請求到來之后調用 accept()函數接受連接請求&#xff0c…

SQL Server【四】

identity 主鍵自動增長&#xff0c;用戶不需要為identity修飾的主鍵賦值 create table student (std_id int primary key identity(10,5),--(10,5)可以省略&#xff0c;默認為(1,1)std_name nvarchar(200) not null ) select * from student insert into student values (張三…

TCP服務器/客戶端實例(C/C )

1.1、Linux下的TCP服務器&#xff1a; #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h>void error_handling(char *mess…

pip代理解決pip下載失敗問題

在用pip下載各種庫的時候發現速度實在是太慢了&#xff0c;還會有各種奇奇怪怪的問題&#xff0c;動不動就玄學失敗。 在網上找來找去找到知乎上一位大佬的回答&#xff1a;傳送門&#xff0c;用了豆瓣的代理。哇咔咔&#xff0c;媽媽再也不用擔心我下載失敗了。 代理&#x…

實現Linux select IO復用C/S服務器代碼

服務器端#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/stat.h> #include<arpa/inet.h> #include <sys/select.h>#define MAXBUF 256 #define MAXLISTEN…

Bellman-Ford算法和SPFA算法

Belloman-Ford算法 算法介紹 Dijkstra可以解決單源無負邊最短路徑問題。但是當遇到含有負邊的單源最短路徑問題就需要使用Bellman-Ford算法來解決。Bellman-Ford算法還可以檢測出負環。 算法步驟 源點s,數組d[u]d[u]d[u]表示s到u的最短距離初始化&#xff1a;d[s]0d[s]0d[s…

C語言實現單鏈表操作

SLIST_H #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #include<assert.h> typedef int ElemType; typedef struct Node { //定義單鏈表中的結點信息 ElemType data; //結點的數據域 struct Node *next; //結點的指針…