/* sq_queue.c
*
* Copyright (c) 2020
* Author: plant
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdio.h>
#define MAX_QUEUE_SIZE (100)
typedef int ELEM_TYPE;
typedef struct _sq_queue
{
ELEM_TYPE data[MAX_QUEUE_SIZE];
int front;
int rear;
} sq_queue;
void sq_queue_init(sq_queue* queue)
{
queue->front = 0;
queue->rear = 0;
return;
}
int sq_queue_length(const sq_queue queue)
{
return (MAX_QUEUE_SIZE - queue.front + queue.rear) % MAX_QUEUE_SIZE;
}
int sq_queue_en(sq_queue* queue, const ELEM_TYPE elem)
{
if ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front) return 0;
queue->data[queue->rear] = elem;
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
return 1;
}
int sq_queue_de(sq_queue* queue, ELEM_TYPE* elem)
{
if (queue->front == queue->rear) return 0;
*elem = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
return 1;
}
队列的顺序存储结构实现
最新推荐文章于 2023-08-11 22:33:28 发布