纯C队列,分享!写得不好,勿喷

本文详细介绍了一种基于C语言的循环队列实现方法,并通过具体示例展示了如何创建、入队、出队及检查队列的状态。此外,还探讨了不同队列模式的选择及其应用场景。

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

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

/*重要*/int g_QueueModeUsed=1;   //此处选择队列类型:1->数据覆盖  0->数据存满停止,待出队!
#define  bool int 
#define  TRUE  1
#define  FALSE 0
#define  Report  printf 
typedef struct queue_data_type_structure 
{
    int  bFlag;
    char chBuf[128];
    char reserve[4];
}QUEUE_DATA_TYPE_STRUCTURE,*PQUEUE_DATA_TYPE_STRUCTURE;
typedef struct queue 
{
    QUEUE_DATA_TYPE_STRUCTURE *pBase;
    int nFront;    //指向队列第一个元素
    int nRear;     //指向队列最后一个元素的下一个元素
    int nMaxSize;  //循环队列的最大存储空间
}QUEUE,*PQUEUE;

/*****************************************************************************
 Prototype    : CreateQueue
 Description  : 创建队列
 Input        : PQUEUE Q      
                int nMaxSize   
  History        :
  1.Date         : 2016/12/6
    Author       : Pan
    Modification : Created function
*****************************************************************************/
void CreateQueue(PQUEUE Q,int nMaxSize)
{
    Q->pBase=(QUEUE_DATA_TYPE_STRUCTURE *)malloc(sizeof(QUEUE_DATA_TYPE_STRUCTURE)*nMaxSize);
    if(NULL==Q->pBase)
    {
        Report("Memory allocation failure");
        return ;
    }
    Q->nFront=0;//初始化参数
    Q->nRear=0;
    Q->nMaxSize=nMaxSize;
}

/*****************************************************************************
 Prototype    : FullQueue
 Description  : 检查队列
 Input        : PQUEUE Q  
 Output       : None 
  History        :
  1.Date         : 2016/12/6
    Author       : Pan
    Modification : Created function
*****************************************************************************/
bool FullQueue(PQUEUE Q)
{
    if(Q->nFront==(Q->nRear+1)%Q->nMaxSize)    //判断循环链表是否满,留一个预留空间不用
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
/*****************************************************************************
 Prototype    : EmptyQueue
 Description  : 检查队列
 Input        : PQUEUE Q  
 Output       : None
  History        :
  1.Date         : 2016/12/6
    Author       : Pan
    Modification : Created function
*****************************************************************************/
bool EmptyQueue(PQUEUE Q)
{
    if(Q->nFront==Q->nRear)    //判断是否为空
        return TRUE;
    else
        return FALSE;
}
/*****************************************************************************
 Prototype    : Enqueue
 Description  : 入队
 Input        : PQUEUE Q                         
                QUEUE_DATA_TYPE_STRUCTURE *pBuf   
  History        :
  1.Date         : 2016/12/6
    Author       : Pan
    Modification : Created function
*****************************************************************************/
bool Enqueue(PQUEUE Q, QUEUE_DATA_TYPE_STRUCTURE *pBuf)
{
    if(FullQueue(Q))
    {
        Report("Queue Full,Do you want to cover\n");
        if(g_QueueModeUsed==1){
            Q->nFront=0;
            Q->nRear=0;    
            //memset(&Q->pBase[Q->nRear],0x00,sizeof(QUEUE_DATA_TYPE_STRUCTURE));
            memcpy(&Q->pBase[Q->nRear],pBuf,sizeof(QUEUE_DATA_TYPE_STRUCTURE));
            Q->nRear=(Q->nRear+1)%Q->nMaxSize;
            return TRUE;
        }
        else{
            return FALSE;
        }        
    }
    else
    {
        memcpy(&Q->pBase[Q->nRear],pBuf,sizeof(QUEUE_DATA_TYPE_STRUCTURE));
        Q->nRear=(Q->nRear+1)%Q->nMaxSize;
        return TRUE;
    }
}
/*****************************************************************************
 Prototype    : Dequeue
 Description  : 出列
 Input        : PQUEUE Q                         
                QUEUE_DATA_TYPE_STRUCTURE *pBuf   
  History        :
  1.Date         : 2016/12/6
    Author       : Pan
    Modification : Created function
*****************************************************************************/
bool Dequeue(PQUEUE Q, QUEUE_DATA_TYPE_STRUCTURE *pBuf)
{
    if(EmptyQueue(Q))
    {
        return FALSE;
    }
    else
    {
        memcpy(pBuf,&Q->pBase[Q->nFront],sizeof(QUEUE_DATA_TYPE_STRUCTURE));
        Q->nFront=(Q->nFront+1)%Q->nMaxSize;
        return TRUE;
    }
}
/*****************************************************************************
 Prototype    : ReleaseQueue
 Description  : 释放
 Input        : PQUEUE Q   
  History        :
  1.Date         : 2016/12/6
    Author       : Pan
    Modification : Created function
*****************************************************************************/
void ReleaseQueue(PQUEUE Q)
{
    free(Q->pBase);
}

void TraverseQueue(PQUEUE Q)
{
    QUEUE_DATA_TYPE_STRUCTURE stDataSpi;    
    int i=Q->nFront;
    Report("The element is in the team:\n");
    while(i%Q->nMaxSize!=Q->nRear)
    {
        memset(&stDataSpi,0x00,sizeof(stDataSpi));
        memcpy(&stDataSpi,&Q->pBase[i],sizeof(stDataSpi));
        Report("No.%d ,%d ;  %s ;",i,stDataSpi.bFlag,stDataSpi.chBuf);
        i++;
    }
    Report("\n");
}
#define QUEUE_MAX_SIZE_BUF_USED 10
int main()
{
    QUEUE g_Queue;
    Report("-Que demo- \n\r");
    CreateQueue(&g_Queue,QUEUE_MAX_SIZE_BUF_USED);
    //Report("Start addr=%X\n\r",g_Queue.pBase);
    QUEUE_DATA_TYPE_STRUCTURE stData_spi;
    int i=0,ret=0;
    for(i; i<QUEUE_MAX_SIZE_BUF_USED+1005;i++)
    {
        memset(&stData_spi,0x00,sizeof(stData_spi));
        stData_spi.bFlag=i;
        strcpy(stData_spi.chBuf,"DEMO");        
           ret=Enqueue(&g_Queue, &stData_spi);
        Report("ret=%d\n",ret);
    }
   
      //TraverseQueue(&g_Queue);
    //Report("Release addr=%X\n\r",g_Queue.pBase);
    QUEUE_DATA_TYPE_STRUCTURE stRevBuf;
    i=0;
    for(i; i<QUEUE_MAX_SIZE_BUF_USED;i++)
    {
        memset(&stRevBuf,0x00,sizeof(stRevBuf));
        Dequeue(&g_Queue , &stRevBuf);
        Report("No.%d   %s ",stRevBuf.bFlag,stRevBuf.chBuf);
    }
    ReleaseQueue(&g_Queue);
    return 0;
}

 

转载于:https://my.oschina.net/panyuanyi/blog/800873

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值