日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个通用纯C队列的实现

發(fā)布時間:2023/11/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个通用纯C队列的实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

隊列并不是很復(fù)雜的數(shù)據(jù)結(jié)構(gòu),但是非常實用,這里實現(xiàn)一個隊列是因為在我的另一篇博客非常精簡的Linux線程池實現(xiàn)中要用到。

?

隊列API定義如下:

?

  • //queue.h

  • ?
  • #ifndef QUEUE_H_INCLUDED

  • #define QUEUE_H_INCLUDED

  • ?
  • typedef struct queue *queue_t;

  • ?
  • queue_t queue_create();

  • ?
  • int queue_isempty(queue_t q);

  • ?
  • void* queue_enqueue(queue_t q, unsigned int bytes);

  • ?
  • void* queue_dequeue(queue_t q);

  • ?
  • void queue_destroy(queue_t q);

  • ?
  • #endif //QUEUE_H_INCLUDED

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

    ?

  • struct type *p;

  • p = malloc(sizeof(struct type));

  • p->a = ...;

  • p->b = ...;

  • p->c = ...;

  • ...

  • queue_enqueue(q, p);

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

    ?

  • struct type *p;

  • p=queue_enqueue(q, sizeof(struct type));

  • p->a = ...;

  • p->b = ...;

  • p->c = ...;

  • ...

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

    ?

    隊列的實現(xiàn)如下:

    ?

  • //queue.c

  • ?
  • #include "queue.h"

  • #include <stdlib.h>

  • ?
  • struct node {

  • void *element;

  • struct node *next;

  • };

  • ?
  • struct queue {

  • struct node front;

  • struct node *tail;

  • };

  • ?
  • queue_t queue_create() {

  • queue_t q;

  • q=(queue_t)malloc(sizeof(struct queue));

  • q->front.element=NULL;

  • q->front.next=NULL;

  • q->tail=&q->front;

  • return q;

  • }

  • ?
  • int queue_isempty(queue_t q) {

  • return &q->front==q->tail;

  • }

  • ?
  • void* queue_enqueue(queue_t q, unsigned int bytes) {

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

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

  • q->tail->next->next=NULL;

  • q->tail=q->tail->next;

  • return q->tail->element;

  • }

  • ?
  • void* queue_dequeue(queue_t q) {

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

  • void *element;

  • if(tmp==NULL) {

  • return NULL;

  • }

  • element=tmp->element;

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

  • free(tmp);

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

  • q->tail=&q->front;

  • }

  • return element;

  • }

  • ?
  • void queue_destroy(queue_t q) {

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

  • while(p!=NULL) {

  • tmp=p;

  • p=p->next;

  • free(tmp);

  • }

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

  • }

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

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的一个通用纯C队列的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。