顺序循环队列 初始化、出队、入队、取队头元素、判空

#include  //顺序循环队列  对尾插入,对头删除,先进先出
#define MaxSize 20
typedef struct
{
	int queue[MaxSize];
	int rear;     //对尾指针
	int front;    //对头指针
	int count;    //计数器
}SeqCQueue;

void QueueInitiate(SeqCQueue *q)   //初始化
{
	q->rear = 0;
	q->front = 0;
	q->count = 0;
}
int QueueNotEmpty(SeqCQueue q)  //判空
{
	if (q.count != 0)
		return 1;
	else  return 0;
}
int QueueAppend(SeqCQueue *q, int x)    //入队列
{
	if (q->count > 0 && q->rear == q->front)
	{
		printf("队列已满,无法插入!\n");
		return 0;
	}
	else
	{
		q->queue[q->rear] = x;
		q->rear = (q->rear + 1) % MaxSize;
		q->count++;
		return 1;
	}
}
int QueueDelete(SeqCQueue *q, int *d)   //出队列
{
	if (q->count == 0)
	{
		printf("队列已空,无数据元素可出队列!\n");
		return 0;
	}
	else
	{
		*d = q->queue[q->front];
		q->front = (q->front + 1) % MaxSize;
		q->count--;
		return 1;
	}
}
int QueueGet(SeqCQueue q, int *d)  //取数据元素
{
	if (q.count == 0)
	{
		printf("队列已空,无数据元素可取!\n");
		return 0;
	}
	else
	{
		*d = q.queue[q.front];
		return 1;
	}
}
int main()
{
	SeqCQueue q;
	int i,x;
	QueueInitiate(&q);
	for (i = 0; i < 10; i++)
	{
		QueueAppend(&q,i+1);
	}
	QueueGet(q,&x);
	printf("取对尾数据元素:%d \n",x);
	printf("依次出队列的顺序序列:\n");
	while (QueueNotEmpty(q))
	{
		QueueDelete(&q,&x);
		printf("%d  ",x);
	}
	printf("\n");
	return 0;
}

你可能感兴趣的:(顺序循环队列 初始化、出队、入队、取队头元素、判空)