前几天在公司开发写了个循环队列,感觉挺好的,没用上可惜了。拿出来秀一秀
队列可以自动增长,就是涨上去需要再trim下。
头文件:
#ifndef _NODECIRCLEQUEUE_H
#define _NODECIRCLEQUEUE_H
typedef enum { false=0, true=!false } bool;
typedef void *Node;
typedef struct NodeCircleQueue_T{
Node *queue;
int head;
int tail;
int initLength;
int maxLength;
}NodeCircleQueue;
typedef void (*NodeFunc) (Node node,void *args);
bool vInitNodeCircleQueue(NodeCircleQueue *cqueue,int initLength,int maxLength);
void vDestoryNodeCircleQueue(NodeCircleQueue *cqueue);
bool vOfferNodeCircleQueue(NodeCircleQueue *cqueue,Node node);
Node vPollNodeCircleQueue(NodeCircleQueue *cqueue);
void vTraversalNodeCircleQueue(NodeCircleQueue *cqueue,NodeFunc func,void *args);
#endif
源文件:
#include "nodeCircleQueue.h"
#include <stdlib.h>
#include <string.h>
bool vInitNodeCircleQueue(NodeCircleQueue *cqueue,int initLength,int maxLength){
cqueue->queue = (Node*)malloc(initLength * sizeof(Node));
if(cqueue->queue == NULL){
return false;
}
cqueue->head = cqueue->tail = 0;
cqueue->initLength = initLength;
cqueue->maxLength = maxLength;
return true;
}
void vDestoryNodeCircleQueue(NodeCircleQueue *cqueue){
free(cqueue->queue);
}
bool vOfferNodeCircleQueue(NodeCircleQueue *cqueue,Node node){
//queue is full
if(cqueue->head == ((cqueue->tail+1) % cqueue->initLength)){
if(cqueue->initLength == cqueue->maxLength)
return false;
Node* newQueue;
int newLength;
newLength = cqueue->initLength * 2;
if(newLength > cqueue->maxLength)
newLength = cqueue->maxLength;
newQueue = (Node*)malloc(newLength * sizeof(Node));
if(newQueue == NULL)
return false;
memcpy(newQueue , cqueue->queue , cqueue->initLength * sizeof(Node));
free(cqueue->queue);
cqueue->initLength = newLength;
cqueue->queue = newQueue;
}
//put node at tail,tail++
*(cqueue->queue + cqueue->tail) = node;
cqueue->tail = (cqueue->tail + 1) % cqueue->initLength;
return true;
}
Node vPollNodeCircleQueue(NodeCircleQueue *cqueue){
//queue is empty
if(cqueue->tail == cqueue->head)
return NULL;
Node node;
//node = head, head++
node = *(cqueue->queue + cqueue->head);
cqueue->head = (cqueue->head + 1) % cqueue->initLength;
return node;
}
void vTraversalNodeCircleQueue(NodeCircleQueue *cqueue,NodeFunc func,void *args){
int ptr;
ptr = cqueue->head;
while(ptr != cqueue->tail){
func(*(cqueue->queue + ptr),args);
ptr = (ptr + 1)% cqueue->initLength;
}
}
分享到:
相关推荐
C语言实现循环队列
C语言实现循环队列的基本运算,建立、入队、出队,简单易懂。
c语言实现的循环队列,附代码,标准实验报告
包含了C语言实现循环队列的代码, 有循环队列的彩图讲解, 便于理解代码.
嵌入式开发中常用的队列实现方式,采用C语言实现队列的入队,出队及打印队列元素
循环队列的C++实现,采用类写的,对数据结构学习者很有用哈
c语言数组 C语言实现使用静态数组实现循环队列源码.zipC语言实现使用静态数组实现循环队列源码.zipC语言实现使用静态数组实现循环队列源码.zipC语言实现使用静态数组实现循环队列源码.zip
c语言循环队列的具体实现,包括主函数。具体很难说的清,自己看了就知道了。
C语言实现循环对列,思路清晰。编译环境VC 2008下可编译通过。
c语言数组 C语言数组_C语言实现使用静态数组实现循环队列
在我们生活中有很多队列的影子,可以说与时间相关的问题,一般都会涉及到队列问题;本文详细介绍了如何使用C语言实现循环队列,下面一起来看看。
C语言实现使用静态数组实现循环队列
该C程序使用循环队列实现了N行杨辉三角的输出,实现简单。 使用VC进行编译即可。
C语言循环队列指针实现的一个例子,适合初学者研究
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。 # 优点 循环队列的一个好处是我们可以利用这个...
这是一个循环队列的c语言实现程序,实现入队、出队等操作
xqueue:基于C语言实现的循环队列缓冲区 https://blog.csdn.net/whik1194/article/details/130047136