遇到数据结构的编程题后要动手画图,先思考出较为简便的思路后再动手开始打代码。此题分析时应该先考虑使用链表还是数组,比较发现,使用链表较难实现获取队尾元素,而使用数组可以解决该问题。
源代码如下:
typedef struct {
int* a;//数组名为a
int front;//记录队头位置编号
int rear;//记录队尾位置编号
int k;//循环队列最多存k个数据
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
obj->a = (int*)malloc(sizeof(int) * (k + 1));//循环队列开辟空间为k+1,但实际只用k个空间,多开辟一个空间防止假溢出
obj->rear = obj->front = 0;
obj->k = k;
return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
assert(obj);
return obj->front == obj->rear;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
assert(obj);
return ((obj->rear + 1) % (obj->k + 1)) == obj->front;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
assert(obj);
if (myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->rear++] = value;
obj->rear %= (obj->k + 1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
assert(obj);
if (myCircularQueueIsEmpty(obj))
{
return false;
}
obj->front++;
obj->front %= obj->k + 1;
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
assert(obj);
if (myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
assert(obj);
if (myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[(obj->rear+obj->k)%(obj->k+1)];
}
void myCircularQueueFree(MyCircularQueue* obj) {
assert(obj);
free(obj->a);
free(obj);
}
效果如下:
在vs2022中调试代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef struct {
int* a;
int front;
int rear;
int k;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
obj->a = (int*)malloc(sizeof(int) * (k + 1));//循环队列开辟空间为k+1,但实际只用k个空间,多开辟一个空间防止假溢出
obj->rear = obj->front = 0;
obj->k = k;
return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
assert(obj);
return obj->front == obj->rear;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
assert(obj);
return ((obj->rear + 1) % (obj->k + 1)) == obj->front;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
assert(obj);
if (myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->rear++] = value;
obj->rear %= (obj->k + 1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
assert(obj);
if (myCircularQueueIsEmpty(obj))
{
return false;
}
obj->front++;
obj->front %= obj->k + 1;
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
assert(obj);
if (myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
assert(obj);
if (myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[(obj->rear+obj->k)%(obj->k+1)];
}
void myCircularQueueFree(MyCircularQueue* obj) {
assert(obj);
free(obj->a);
free(obj);
}
int main()
{
MyCircularQueue* ob= myCircularQueueCreate(4);
printf("%d\n",myCircularQueueEnQueue(ob, 1));
myCircularQueueEnQueue(ob, 6);
myCircularQueueEnQueue(ob, 7);
myCircularQueueEnQueue(ob, 8);
printf("%d", myCircularQueueRear(ob));
return 0;
}
效果如下:
欢迎斧正,如有疑惑可私信或评论,会尽力解答,大家一起加油啊!