线性队列
2015-12-22 22:46
274 查看
这里的线性结构实际上指的就是连续内存的意思,只不过使用“线性”这个词显得比较专业而已。前面一篇博客介绍了现象结构的处理方法,那么在这个基础之上我们是不是添加一些属性形成一种新的数据结构类型呢?答案是肯定的,队列便是其中的一种。
队列的性质很简单:
(1)队列有头部和尾部
(2)队列从尾部压入数据
(3)队列从头部弹出数据
那么连续内存下的队列是怎么实现的呢?
a)设计队列数据结构
[cpp] view
plaincopy
typedef struct _QUEUE_NODE
{
int* pData;
int length;
int head ;
int tail;
int count;
}QUEUE_NODE;
b)申请队列内存
[cpp] view
plaincopy
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;
}
c)释放队列内存
[cpp] view
plaincopy
STATUS delete_queue(const QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return FALSE;
assert(NULL != pQueueNode->pData);
free(pQueueNode->pData);
free((void*)pQueueNode);
return TRUE;
}
d)把数据压入队列
[cpp] view
plaincopy
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;
}
e)把数据弹出队列
[cpp] view
plaincopy
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;
}
f)统计当前队列中有多少数据
[cpp] view
plaincopy
int get_total_number(const QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return 0;
return pQueueNode->count;
}
g)查看队列中初始化的时候总长度是多少
[cpp] view
plaincopy
int get_total_number(const QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return 0;
return pQueueNode->length;
队列的性质很简单:
(1)队列有头部和尾部
(2)队列从尾部压入数据
(3)队列从头部弹出数据
那么连续内存下的队列是怎么实现的呢?
a)设计队列数据结构
[cpp] view
plaincopy
typedef struct _QUEUE_NODE
{
int* pData;
int length;
int head ;
int tail;
int count;
}QUEUE_NODE;
b)申请队列内存
[cpp] view
plaincopy
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;
}
c)释放队列内存
[cpp] view
plaincopy
STATUS delete_queue(const QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return FALSE;
assert(NULL != pQueueNode->pData);
free(pQueueNode->pData);
free((void*)pQueueNode);
return TRUE;
}
d)把数据压入队列
[cpp] view
plaincopy
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;
}
e)把数据弹出队列
[cpp] view
plaincopy
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;
}
f)统计当前队列中有多少数据
[cpp] view
plaincopy
int get_total_number(const QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return 0;
return pQueueNode->count;
}
g)查看队列中初始化的时候总长度是多少
[cpp] view
plaincopy
int get_total_number(const QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return 0;
return pQueueNode->length;
相关文章推荐
- Oracle的REGEXP_INSTR函数简单用法
- 心上莲花:中医三五事
- Oracle的REGEXP_INSTR函数简单用法
- Oracle的REGEXP_INSTR函数简单用法
- 无盘网吧电脑的主板怎么设置才能用硬盘启动?
- 第一篇大数据学习心得
- iOS 应用内付费
- OC中堆和栈的区别
- 2015第53周二
- 《第9章 循环结构进阶》
- POJ 2752 Milking Grid
- java异常
- 蓝桥考试资料汇总
- 七牛---Android SDK进行代码混淆配置
- 静态 static
- birt报表插件,实现数据报表
- 有个东西叫梦想
- 单片机串口中断
- C读取和写入一个二维数组
- 解决Windwos Server 2008缓存耗用大量物理内存