队列的特点是遵循“先进先出”规则的受限制的顺序表,在处理过程中需要注意“假溢出”问题的处理。
seq_queue.h
#ifndef _SEQ_QUEUE_H
#define _SEQ_QUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUEUE_ERR (-1)
#define QUEUE_SUCCESS 0
#define QUEUE_MAX_SIZE 128
#define DATA_TYPE_IS_INT
#if defined DATA_TYPE_IS_CHAR
typedef char DataType;
#elif defined DATA_TYPE_IS_INT
typedef int DataType;
#elif defined DATA_TYPE_IS_FLOAT
typedef float DataType;
#endif
typedef struct seq_queue {
int front; //队首元素下标
int rear; //队尾元素下标
DataType *data; //数据区
}*PQueue, Queue;
/* 创建一个队列 */
PQueue CreateQueue();
/* 获取一个队列的长度 */
int GetQueueLength(const PQueue queue);
/* 判断一个队列是否为空 */
int QueueIsEmpty(const PQueue queue);
/* 判断一个队列是否队满 */
int QueueIsFull(const PQueue queue);
/* 向队列尾部插入一个数据 */
int InsertToQueueRear(PQueue queue, const DataType data);
/* 将队首元素出队列 */
int DeleteFromQueueFront(PQueue queue);
/* 获取队列头部元素而不出列 */
DataType GetFrontElement(const PQueue queue);
/* 将一个队列清空 */
void ClearQueue(PQueue queue);
/* 销毁一个队列 */
void DestroyQueue(PQueue queue);
#endif
seq_queue.c
#include "seq_queue.h"
PQueue CreateQueue()
{
PQueue queue = (PQueue)malloc(sizeof(Queue));
if(queue == NULL) {
perror("queue malloc error.");
return (PQueue)QUEUE_ERR;
}
queue->data = (DataType*)malloc(sizeof(DataType) * QUEUE_MAX_SIZE);
if(queue->data == NULL) {
perror("queue data malloc error.");
free(queue);
return (PQueue)QUEUE_ERR;
}
queue->front = 0;
queue->rear = 0;
return queue;
}
int GetQueueLength(const PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return QUEUE_ERR;
}
return (queue->rear - queue->front);
}
int QueueIsEmpty(const PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return QUEUE_ERR;
}
return (queue->rear == queue->front);
}
int QueueIsFull(const PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return QUEUE_ERR;
}
return ((queue->rear - queue->front) == QUEUE_MAX_SIZE);
}
int InsertToQueueRear(PQueue queue, const DataType data)
{
if(queue == NULL) {
perror("queue is null.");
return QUEUE_ERR;
}
if((queue->rear - queue->front) == QUEUE_MAX_SIZE) {
perror("queue is full, no space insert.");
return QUEUE_ERR;
}
if(queue->rear == QUEUE_MAX_SIZE) {
//move each element of the queue to the begining (position 0)
int distance = queue->front;
for(int i=queue->front; i<QUEUE_MAX_SIZE; i++) {
queue->data[i-distance] = queue->data[i];
}
queue->front = 0;
queue->rear -= distance;
}
queue->data[queue->rear++] = data;
return QUEUE_SUCCESS;
}
int DeleteFromQueueFront(PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return QUEUE_ERR;
}
if(queue->rear == 0) {
perror("queue is empty, no element return.");
return QUEUE_ERR;
}
if(queue->front == (QUEUE_MAX_SIZE - 1)) {
queue->front = 0;
queue->rear = 0;
}
queue->front++;
return QUEUE_SUCCESS;
}
DataType GetFrontElement(const PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return (DataType)QUEUE_ERR;
}
if(queue->rear == 0) {
perror("queue is empty, no element return.");
return (DataType)QUEUE_ERR;
}
return queue->data[queue->front];
}
void ClearQueue(PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return;
}
if(queue->rear == 0) {
printf("queue is empty, do not need clear.\n");
return;
}
queue->front = queue->rear = 0;
return;
}
void DestroyQueue(PQueue queue)
{
if(queue == NULL) {
perror("queue is null.");
return;
}
free(queue->data);
free(queue);
queue = NULL;
printf("destroy queue completd.\n");
return;
}
main.c
#include "seq_queue.c"
int main(void)
{
int ret;
PQueue myQueue = CreateQueue();
for(int i=0; i<128; i++) {
ret = InsertToQueueRear(myQueue, i+1);
if(ret == QUEUE_ERR) {
perror("insert into queue error.");
return QUEUE_ERR;
}
}
printf("queue is empty?[%s]\n", QueueIsEmpty(myQueue)?"yes":"no");
printf("queue is full?[%s]\n", QueueIsFull(myQueue)?"yes":"no");
DeleteFromQueueFront(myQueue);
printf("queue is full?[%s]\n", QueueIsFull(myQueue)?"yes":"no");
printf("length of queue is:%d\n", GetQueueLength(myQueue));
printf("front element is:%d\n", GetFrontElement(myQueue));
ClearQueue(myQueue);
DestroyQueue(myQueue);
return 0;
}