循环队列c++代码
#include <ros/ros.h> #include <string> #include <stdlib.h> #include <iostream> #include <sys/msg.h> #include <cstdio> #include <cstdlib> #include <sys/types.h> #include <sys/ipc.h> #include <unistd.h> using namespace std; typedef struct _QUEUE_NODE { int *pData; int length; int head; int tail; int count; }QUEUE_NODE; QUEUE_NODE* alloca_queue(QUEUE_NODE* pQueue_Node,int number) { if(0 == number) return NULL; pQueue_Node = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE)); memset(pQueue_Node, 0, sizeof(QUEUE_NODE)); pQueue_Node->pData = (int*)malloc(sizeof(int)*number); memset(pQueue_Node->pData, 0, sizeof(pQueue_Node->pData)); if(NULL == pQueue_Node->pData) { free(pQueue_Node); return NULL; } pQueue_Node->length = number; return pQueue_Node; } bool delete_queue(const QUEUE_NODE* pQueue_Node) { if(NULL == pQueue_Node) return false; free(pQueue_Node->pData); free((void*)pQueue_Node); return true; } bool insert_queue(QUEUE_NODE* pQueue_Node, int value) { if(NULL == pQueue_Node) return false; if(pQueue_Node->length == pQueue_Node->count) return false; pQueue_Node->pData[pQueue_Node->tail] = value; pQueue_Node->tail = (pQueue_Node->tail + 1) % pQueue_Node->length; pQueue_Node->count ++; return true; } int main(int argc, char **argv) { std::cout<<"test qusetion9 start !"<<std::endl; ros::init(argc,argv,"question9_node"); ros::NodeHandle node; ros::Rate r(1); QUEUE_NODE* qQueue_node; qQueue_node = alloca_queue(qQueue_node,3); cout << "size is : " << qQueue_node->length << endl; cout << "tail is : " << qQueue_node->tail << endl; cout << "head is : " << qQueue_node->head << endl; cout << "pData->tail is : " << qQueue_node->pData[qQueue_node->tail] << endl; if(insert_queue(qQueue_node,200) == true) { cout << "size is : " << qQueue_node->length << endl; cout << "data is : " << qQueue_node->pData[qQueue_node->tail] << endl; } if(insert_queue(qQueue_node,300) == true) { cout << "size is : " << qQueue_node->length << endl; cout << "data is : " << qQueue_node->pData[qQueue_node->head+1] << endl; } }
posted on 2019-02-15 10:25 kuangxionghui 阅读(227) 评论(0) 编辑 收藏 举报