C语言--循环队列实现


#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
/*
	循环队列
*/

typedef struct Queue
{
	int *p_base;
	int front;
	int rear;
} QUEUE,*PQUEUE;

void init_queue(QUEUE *);//队列初始化

bool en_queue(QUEUE *, int); //加入队列

void traverse_queue(QUEUE *);

bool is_full(QUEUE *);

bool is_empty(QUEUE *);

bool out_queue(QUEUE *, int *);


int main()
{
	QUEUE queue;
	int value;
	init_queue(&queue);
	en_queue(&queue, 1);
	en_queue(&queue, 1);
	en_queue(&queue, 1);
	en_queue(&queue, 1);
	en_queue(&queue, 1);
	traverse_queue(&queue);
	printf("\n");
	out_queue(&queue, &value);
	printf("%d\n", value);
	traverse_queue(&queue);
	return 0;
}

void init_queue(QUEUE *p_queue)
{
	p_queue->p_base = (int *)malloc(sizeof(int) * 6);
	p_queue->front = 0;
	p_queue->rear = 0;

	return;
}

bool en_queue(QUEUE * p_queue, int value)
{
	if (is_full(p_queue))
	{
		return false;
	}
	else
	{
		p_queue->p_base[p_queue->rear] = value;
		p_queue->rear = (p_queue->rear + 1) % 6;
		return true;
	}

}

bool is_full(QUEUE *p_queue)
{
	if ((p_queue->rear + 1) % 6 == p_queue->front)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void traverse_queue(QUEUE *p_queue)
{
	int i = p_queue->front;

	while (i != p_queue->rear)
	{
		printf("%d ", p_queue->p_base[i]);
		i = (i + 1) % 6;
	}
	return;
}

bool is_empty(QUEUE *p_queue)
{
	if (p_queue->front == p_queue->rear)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool out_queue(QUEUE *p_queue, int *p_value)
{
	if (is_empty(p_queue))
	{
		return false;
	}
	else
	{
		*p_value = p_queue->p_base[p_queue->front];
		p_queue->front = (p_queue->front + 1) % 6;
		return false;
	}
}

转载于:https://www.cnblogs.com/ykyk1229/p/9808794.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言实现循环队列的方法如下: 1.定义一个结构体来表示循环队列,包括队列的大小、队首和队尾指针以及存储元素的数组。 ```c typedef struct { int* arr; // 存储元素的数组 int front; // 队首指针 int rear; // 队尾指针 int count; // 队列中元素的个数 int N; // 队列的大小 } MyCircularQueue; ``` 2.创建循环队列的函数,需要动态分配内存空间,并初始化队列的各个参数。 ```c MyCircularQueue* myCircularQueueCreate(int k) { MyCircularQueue* q = (MyCircularQueue*)malloc(sizeof(MyCircularQueue)); // 申请空间 if (q == NULL) { return NULL; } q->arr = (int*)malloc(sizeof(int) * k); // 申请空间 if (q->arr == NULL) { return NULL; } q->front = 0; // 初始化 q->rear = 0; q->count = 0; q->N = k; return q; } ``` 3.判断队列是否为空的函数,当队列中元素个数为0时,队列为空。 ```c bool myCircularQueueIsEmpty(MyCircularQueue* obj) { return obj->count == 0; } ``` 4.判断队列是否已满的函数,当队列中元素个数等于队列大小时,队列已满。 ```c bool myCircularQueueIsFull(MyCircularQueue* obj) { return obj->count == obj->N; } ``` 5.向队列中插入元素的函数,需要先判断队列是否已满,如果已满则插入失败,否则将元素插入队尾。 ```c bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) { if (myCircularQueueIsFull(obj)) { return false; } obj->arr[obj->rear] = value; obj->rear = (obj->rear + 1) % obj->N; obj->count++; return true; } ``` 6.从队列中删除元素的函数,需要先判断队列是否为空,如果为空则删除失败,否则将队首元素删除。 ```c bool myCircularQueueDeQueue(MyCircularQueue* obj) { if (myCircularQueueIsEmpty(obj)) { return false; } obj->front = (obj->front + 1) % obj->N; obj->count--; return true; } ``` 7.获取队首元素的函数,需要先判断队列是否为空,如果为空则返回-1,否则返回队首元素。 ```c int myCircularQueueFront(MyCircularQueue* obj) { if (myCircularQueueIsEmpty(obj)) { return -1; } return obj->arr[obj->front]; } ``` 8.获取队尾元素的函数,需要先判断队列是否为空,如果为空则返回-1,否则返回队尾元素。 ```c int myCircularQueueRear(MyCircularQueue* obj) { if (myCircularQueueIsEmpty(obj)) { return -1; } return obj->arr[(obj->rear - 1 + obj->N) % obj->N]; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值