一、题目描述
二、提供方便走读代码的图
三、题目代码
顺序表实现
typedef struct {
int* a;
int front;
int tail;
int k;
} MyCircularQueue;
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* cq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
cq->a = (int*)malloc(sizeof(int)*(k+1));
cq->front = cq->tail = 0;
cq->k = k;
return cq;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))return false;
obj->a[obj->tail] = value;
obj->tail++;
obj->tail %= (obj->k+1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))return false;
obj->front++;
obj->front %= (obj->k+1);
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))return -1;
return obj->a[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))return -1;
if(obj->tail == 0)
return obj->a[obj->k];
else
return obj->a[obj->tail-1];
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->front == obj->tail;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->tail+1)%(obj->k+1) == obj->front;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
free(obj);
}
链表实现
typedef struct Queuenode{
struct Queuenode* next;
int data;
} Queuenode;
typedef struct {
struct Queuenode* head;
struct Queuenode* tail;
} MyCircularQueue;
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* cq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
Queuenode* newnode = (Queuenode*)malloc(sizeof(Queuenode));
newnode->next=newnode;
cq->head = cq->tail = newnode;
for(int i=0 ; i < k; i++)
{
Queuenode* knode = (Queuenode*)malloc(sizeof(Queuenode));
cq->tail->next=knode;
knode->next=cq->head;
cq->tail=knode;
}
cq->tail=cq->head;
return cq;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))return false;
obj->tail->data = value;
obj->tail = obj->tail->next;
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))return false;
obj->head = obj->head->next;
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))return -1;
return obj->head->data;
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))return -1;
Queuenode* cur = obj->head;
while(cur->next != obj->tail)
{
cur = cur->next;
}
return cur->data;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->head == obj->tail;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return obj->tail->next == obj->head;
}
void myCircularQueueFree(MyCircularQueue* obj) {
Queuenode* cur = obj->head->next;
Queuenode* head = obj->head;
Queuenode* p = NULL;
free(obj->head);
while(cur != head)
{
p = cur;
cur = cur->next;
free(p);
}
free(obj);
}