//循环队列可存储数据数量是maxsize-1
//队列长度为(front-rear+maxsize)%maxsize
//队列为空时front==rear
//队列满时 (front+1)%maxsize==rear;
#define MAXSIZE 5
#include<iostream>
using namespace std;
typedef struct queue {
int front;
int rear;
int data[MAXSIZE];
}queue;
queue* initqueue()
{
queue* q = new queue;
q->rear = 0;
q->front = 0;
return q;
}
bool isfull(queue* q)
{
if ((q->front + 1) % MAXSIZE == q->rear)
{
return true;
}
else
return false;
}
void push(queue* q,int data)
{
if (isfull(q))
{
return;
}
else {
q->data[q->front] = data;
q->front = (q->front + 1) % MAXSIZE;
}
}
bool isempty(queue* q)
{
if (q->front == q->rear)
{
return true;
}
else
return false;
}
int pop(queue* q)
{
if (isempty(q))
{
return -1;
}
else {
int data = q->data[q->rear];
q->rear = (q->rear + 1) % MAXSIZE;
return data;
}
}
void print(queue* q)
{
int len = (q->front - q->rear + MAXSIZE) % MAXSIZE;
int index = q->rear;
for (int i = 0; i < len; i++)
{
cout << q->data[index] << "->";
index = (index + 1) % MAXSIZE;
}
cout << "NULL" << endl;
}
int main()
{
queue* q = initqueue();
push(q, 1);
push(q, 2);
push(q, 3);
push(q, 4);
push(q, 5);
push(q, 6);
push(q, 7);
print(q);
int i = pop(q);
pop(q);
print(q);
return 0;
}
标签:24,return,int,queue,MAXSIZE,front,打卡,数据结构,rear
From: https://www.cnblogs.com/wlxdaydayup/p/17428044.html