重新教自己学算法之线性队列(七)

线性结构实际上指的就是连续内存的意思。
队列的性质很简单:
(1)队列有头部和尾部
(2)队列从尾部压入数据
(3)队列从头部弹出数据
那么连续内存下的队列是怎么实现的呢?

#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
using namespace std;

//结构体的定义
typedef struct _QUEUE_NODE
{
    int* pData;
    int length;
    int head;
    int tail;
    int count;
}QUEUE_NODE;
#define STATUS int
#define TRUE 1
#define FALSE 0

//申请队列内存
QUEUE_NODE* alloca_queue(int number)
{
    QUEUE_NODE* pQueueNode;
    if( 0 == number)
        return NULL;

    pQueueNode = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE));
    assert(NULL != pQueueNode);
    memset(pQueueNode, 0 , sizeof(QUEUE_NODE));

    pQueueNode->pData = (int*)malloc(sizeof(int) * number);
    if(NULL == pQueueNode->pData)
    {
        free(pQueueNode);
        return NULL;
    }

    pQueueNode->length = number;
    return pQueueNode;
}

//释放队列内存
STATUS delete_queue(const QUEUE_NODE* pQueueNode)
{
    if(NULL == pQueueNode)
        return FALSE;
    assert(NULL != pQueueNode->pData);

    free(pQueueNode->pData);
    free((void*)pQueueNode);
    return TRUE;
}
//把数据压入堆栈
STATUS insert_queue(QUEUE_NODE* pQueueNode, int value)
{
    if(NULL == pQueueNode)
        return FALSE;

    if(pQueueNode->length == pQueueNode->count)
        return FALSE;

    pQueueNode->pData[pQueueNode->tail] = value;
    pQueueNode->tail = (pQueueNode->tail + 1) % pQueueNode->length;
    pQueueNode->count++;
    return TRUE; 
}
//把数据弹出队列
STATUS get_queue_data(QUEUE_NODE* pQueueNode, int* value)  
{  
    if(NULL == pQueueNode || NULL == value)  
        return FALSE;  

    if(0 == pQueueNode->count)  
        return FALSE;  

    *value = pQueueNode->pData[pQueueNode->head];  
    pQueueNode-> pData[pQueueNode->head] = 0;   
    pQueueNode-> count --;  
    pQueueNode->head = (pQueueNode->head + 1) % pQueueNode->length;  
    return TRUE;  
}
 //统计当前队列中有多少数据
int  get_total_number(const QUEUE_NODE* pQueueNode)  
{  
    if(NULL == pQueueNode)  
        return 0;  

    return pQueueNode->count;  
}  
//查看队列中初始化的时候总长度是多少
int  get_total_length(const QUEUE_NODE* pQueueNode)  
{  
    if(NULL == pQueueNode)  
        return 0;  

    return pQueueNode->length;  
}
void output_queue(const QUEUE_NODE* pQueueNode)
{
    for (int i = 0; i < pQueueNode->length; ++i)
    {
        cout<<pQueueNode->pData[i]<<"  ";
    }
    cout<<endl;
} 
int main(int argc, char const *argv[])
{
    QUEUE_NODE* pQueueNode = alloca_queue(3);
    insert_queue(pQueueNode,18);
    insert_queue(pQueueNode,20);
    cout<<get_total_number(pQueueNode)<<endl;
    cout<<get_total_length(pQueueNode)<<endl;
    insert_queue(pQueueNode,2);
    int *number;
    get_queue_data(pQueueNode,number);
    cout<<*number<<endl;
    insert_queue(pQueueNode,22);
    output_queue(pQueueNode);
    get_queue_data(pQueueNode,number);
    output_queue(pQueueNode);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值