循环队列oj
这个题可以选择两种实现方式 使用来链表 使用数组
有什么区别呢
首先循环队列要循环 链表可以直接指向 数组需要下标回到0实现循环
然后就是实现以上功能
要获取队尾元素 数组可以直接使用下标 链表的话要使用双链表
链表实现:
typedef struct queuenode
{
struct queuenode *prev;
struct queuenode *next;
int data;
}queuenode;
typedef struct {
//哨兵
queuenode* phead;
int capacity;
} MyCircularQueue;
//尾插节点
void pushback(queuenode* phead)
{
queuenode*cur=phead;
int x=cur->data;
while(x)
{
cur=cur->next;
x--;
}
queuenode* newnode=(queuenode*)malloc(sizeof(queuenode));
newnode->next=cur->next;
cur->next->prev=newnode;
newnode->prev=cur;
cur->next=newnode;
newnode->data=0;
}
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* mydqueue=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
mydqueue->phead=(queuenode*)malloc(sizeof(queuenode));
mydqueue->phead->data=0;
mydqueue->phead->prev=mydqueue->phead;
mydqueue->phead->next=mydqueue->phead;
mydqueue->capacity=k;
while(k--)
{
pushback(mydqueue->phead);
}
return mydqueue;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(obj->capacity > obj->phead->data)
{
queuenode* cur=obj->phead;
obj->phead->data++;
int x=obj->phead->data;
while(x--)
{
cur=cur->next;
}
cur->data=value;
}
else
return false;
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(obj->phead->data==0)
return false;
obj->phead->data--;
int x=obj->phead->data;
queuenode* cur=obj->phead->next;
while(x--)
{
cur->data=cur->next->data;
cur=cur->next;
}
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(obj->phead->data==0)
return -1;
else
return obj->phead->next->data;
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(obj->phead->data==0)
{
return -1;
}
queuenode* cur=obj->phead;
int x=obj->phead->data;
while(x--)
{
cur=cur->next;
}
return cur->data;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
if(obj->phead->data)
return false;
return true;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
if(obj->phead->data==obj->capacity)
return true;
return false;
}
void myCircularQueueFree(MyCircularQueue* obj) {
int x=obj->capacity;
while(x--)
{
queuenode*node=obj->phead;
obj->phead=obj->phead->next;
free(node);
}
free(obj);
}
数组实现:
typedef struct {
int* queue;
int front;
int rear;
int k
} MyCircularQueue;
/** Initialize your data structure here. Set the size of the queue to be k. */
//创建一个可以存放k个元素的循环队列,实际申请的空间为k + 1
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* pcq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
pcq->queue = (int*)malloc(sizeof(int)*(k+1));
pcq->front = 0;
pcq->rear = 0;
pcq->k = k;
return pcq;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
//判满
if((obj->rear+1)%(obj->k+1) == obj->front)
{
return false;
}
//队尾入队
obj->queue[obj->rear++] = value;
//如果队尾越界,更新为最小值
if(obj->rear == obj->k+1)
obj->rear = 0;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
//判空
if(obj->front == obj->rear)
return false;
//队头出队
++obj->front;
//如果队头越界,更新为最小值
if(obj->front == obj->k+1)
obj->front = 0;
return true;
}
/** Get the front item from the queue. */
int myCircularQueueFront(MyCircularQueue* obj) {
if(obj->front == obj->rear)
return -1;
else
return obj->queue[obj->front];
}
/** Get the last item from the queue. */
int myCircularQueueRear(MyCircularQueue* obj) {
if(obj->front == obj->rear)
return -1;
//队尾元素再rear索引的前一个位置,如果rear为0,
//则队尾元素在数组的最后一个位置
if(obj->rear == 0)
return obj->queue[obj->k];
else
return obj->queue[obj->rear-1];
}
/** Checks whether the circular queue is empty or not. */
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->front == obj->rear;
}
/** Checks whether the circular queue is full or not. */
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->rear+1)%(obj->k+1) == obj->front;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->queue);
free(obj);
}