隊列的實現

隊列,也稱 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章