队列的实现

本文介绍了一种简单的FIFO(先进先出)队列数据结构实现,并提供了C语言的具体实现代码。该队列包括创建、销毁、入队和出队等基本操作,并使用互斥锁和条件变量确保线程安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列,也称 FIFO(first in first out ),队列在队尾插入数据,在对头删除数据,即这两个操作要在两端分别操作。实现很简单,直接上代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "hi3531/include/playrtmp/dataQueue.h"




queue* createQueue()   //创建队列
{
//    return (queue *)malloc(sizeof(queue));
    printf("------------->> [createQueue]  create a queue \n");
    queue * Q = (queue *)malloc(sizeof(queue));
    if(Q == NULL)
    {
        printf("------------>>> malloc queue failed!\n");
        return NULL;
    }
    Q->front = Q->rear = (qNode*)malloc(sizeof(qNode));
    if(Q->front == NULL)
    {
        printf("--------------------->>> malloc front of queue failed! \n");
        return NULL;
    }
    if(0 != pthread_mutex_init(&Q->mutex,NULL))    //初始化互斥锁
    {
        printf("create queue failed!\n");
        return NULL;
    }
    if( 0 != pthread_cond_init(&Q->wake_event, NULL))
    {
        printf("create queue wake event failed!\n");
        return NULL;
    }
    Q->front->next = NULL;
    return Q;
}




void destroyQueue(queue * pQueue)
{
    printf("[destroyQueue]  destroy queue !\n");
    if(pQueue == NULL)
    {
        printf("input queue not exist, please check!\n");
        return;
    }


    while (pQueue->front != NULL) {
        pQueue->rear = pQueue->front->next;
        free(pQueue->front);
        pQueue->front = pQueue->rear;
    }
    pthread_mutex_destroy(&pQueue->mutex);
    free(pQueue);
    pQueue = NULL;


}


int isEmptyQueue(queue* Q)
{
    if (Q->front == Q->rear) {
//        printf("队列为空...\n");
        return 1;
    }
    else {
        //printf("队列不为空...\n");
        return 0;
    }
}


//从队尾插入数据
void inQueue(queue *pQueue, unsigned char  *in_buf, int buf_lenth) //入队
{
    if(in_buf == NULL)
    {
        printf("input data is none, please check!\n");
        return;
    }
    if(pQueue == NULL)
    {
        printf("input is a NULL queue, it will be created\n");
        return;
    }


    pthread_mutex_lock(&pQueue->mutex);


        qNode * node = (qNode *)malloc(sizeof(qNode));   //非空队列,创建一个节点
        node->data = (unsigned char *)malloc( buf_lenth /*strlen(in_buf)*/ );
        memcpy(node->data, in_buf, buf_lenth /*strlen(in_buf)*/ );
        node->iLenth = buf_lenth;
        node->next = NULL;


        pQueue->rear->next = node;


        //队列往后移动
        pQueue->rear = node;


        pthread_cond_signal(&pQueue->wake_event);
    pthread_mutex_unlock(&pQueue->mutex);
}


//从队头输出数据
qNode* outQueue(queue *pQueue)   //出队
{


    if (isEmptyQueue(pQueue)) {
//        printf("empty queue......\n");
        return NULL;
    }


    pthread_mutex_lock(&pQueue->mutex);


    qNode* node = NULL;
    node = pQueue->front->next;
    pQueue->front->next = node->next;


    if(pQueue->rear == node)
        pQueue->rear = pQueue->front;


    pthread_mutex_unlock(&pQueue->mutex);


    return node;
}




//for test
int main_test(void)
{
    unsigned char data[5] = "1234";
    unsigned char data1[5] = "4321";


    queue *Q = createQueue();


    inQueue(Q, data, 5);
    inQueue(Q, data1, 5);


    int i = 0;
    for(; i < 2; i++)
    {
        qNode node = outQueue(Q);
        printf("node data is :%d, length is :%d \n", node.data, node.iLenth);
        free(node.data);
        free(node);
    }


    destroyQueue(Q);


    return 0;
}



下面是头文件

#ifndef DATAQUEUE_H
#define DATAQUEUE_H


#include <pthread.h>


typedef struct queue_node{


    unsigned char *data;


    int iLenth;


    struct queue_node *next;


}qNode;


typedef struct queue{


    pthread_mutex_t     mutex;


    qNode *front, *rear;


    pthread_cond_t       wake_event;


}queue;




queue* createQueue();   //创建队列


void destroyQueue(queue * pQueue);  //销毁队列


void inQueue(queue *pQueue, unsigned char *in_buf, int buf_lenth); //入队


qNode* outQueue(queue *pQueue);   //出队


#endif // DATAQUEUE_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值