1 Star 9 Fork 2

Lamdonn/queue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
queue.h 6.46 KB
一键复制 编辑 原始数据 按行查看 历史
Lamdonn 提交于 2024-11-27 18:27 +08:00 . Add insert and erase functions
/*********************************************************************************************************
* ------------------------------------------------------------------------------------------------------
* file description
* ------------------------------------------------------------------------------------------------------
* \file queue.h
* \brief This is a C language queue
* \author Lamdonn
* \version 1.1.0
********************************************************************************************************/
/*********************************************************************************************************
MIT License
Copyright (C) 2023 Lamdonn.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
********************************************************************************************************/
#ifndef __queue_H
#define __queue_H
/* queue type define */
typedef struct QUEUE
{
void* base; /* Protected: base address of data */
int dsize; /* Protected: size of queue data */
int capacity; /* Protected: capacity of queue */
int size; /* Protected: size of queue */
int head; /* Protected: index of queue head */
int tail; /* Protected: index of queue tail */
} *queue;
/**
* \brief define queue.
* \param[in] type: the data type of the queue
* \param[in] capacity: the capacity of queue
* \return queue
*/
#define queue(type, capacity) (&(struct QUEUE){(type[capacity]){0},sizeof(type),capacity,0,0,0})
/**
* \brief push data into the queue.
* \param[in] queue: queue
* \param[in] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
int queue_push(queue queue, void* data);
/**
* \brief push data into the queue, even if the capacity is full, old data will be removed to ensure new data is pushed
* \param[in] queue: queue
* \param[in] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
int queue_push2(queue queue, void* data);
/**
* \brief pop data from the queue.
* \param[in] queue: queue
* \param[out] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
int queue_pop(queue queue, void* data);
/**
* \brief insert data to the specified index of queue.
* \param[in] queue: queue
* \param[in] index: specifying an index
* \param[out] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
int queue_insert(queue queue, int index, void* data);
/**
* \brief erase data from the specified index of queue.
* \param[in] queue: queue
* \param[in] index: specified index
* \param[out] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
int queue_erase(queue queue, int index, void* data);
/**
* \brief clear queue.
* \param[in] queue: queue
* \return none
*/
void queue_clear(queue queue);
/**
* \brief get data address of queue.
* \param[in] queue: queue
* \param[in] index: index
* \return address of queue data or 0 fail
*/
void* queue_data(queue queue, int index);
/**
* \brief macro definition method for get front item of queue
* \param[in] queue: queue
* \param[in] type: tpye of item
* \param[in] i: index
* \return reference to the specified item
*/
#define queue_at(queue, type, i) (*(type *)queue_data((queue),(i)))
/**
* \brief push data into the queue from the front.
* \param[in] queue: queue
* \param[in] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
#define queue_push_front(queue, data) queue_insert((queue), 0, data)
/**
* \brief push data into the queue from the back.
* \param[in] queue: queue
* \param[in] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
#define queue_push_back(queue, data) queue_push((queue), data)
/**
* \brief pop data from the front of queue.
* \param[in] queue: queue
* \param[out] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
#define queue_pop_front(queue, data) queue_pop((queue), data)
/**
* \brief pop data from the back of queue.
* \param[in] queue: queue
* \param[out] data: the address of data, if it is 0, no value will be assigned
* \return 1 success or 0 fail
*/
#define queue_pop_back(queue, data) queue_erase((queue), (queue)->size - 1, data)
/**
* \brief get size of queue.
* \param[in] queue: queue
* \return size of queue
*/
#define queue_size(queue) ((queue)->size)
/**
* \brief get capacity of queue.
* \param[in] queue: queue
* \return size of queue
*/
#define queue_capacity(queue) ((queue)->capacity)
/**
* \brief check if empty.
* \param[in] queue: queue
* \return 0 not empty or others empty
*/
#define queue_empty(queue) ((queue)->size == 0)
/**
* \brief check if full.
* \param[in] queue: queue
* \return 0 not full or others full
*/
#define queue_full(queue) ((queue)->size == (queue)->capacity)
/**
* \brief convert literals to addresses
* \param[in] type: type, such as int/char/float etc.
* \param[in] value: literal value
* \return address of literal
*/
#ifndef literal
#define literal(type, value) ((type[1]){value})
#endif
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/Lamdonn/queue.git
git@gitee.com:Lamdonn/queue.git
Lamdonn
queue
queue
master

搜索帮助