代码拉取完成,页面将自动刷新
/*********************************************************************************************************
* ------------------------------------------------------------------------------------------------------
* 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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。