#define SIZE 100
typedef struct
{
int data[SIZE];
int front;
int rear;
int size;
}queue;
//初始化
void queueinit(queue* ps)
{
ps->front = ps->rear = 0;
ps->size = 0;
}
//判空
bool Empty(queue* ps)
{
return ps->size == 0;
}
//队满
bool isFull(queue* ps)
{
return ps->size == SIZE;
}
//从队尾删除元素
int pop_rear(queue* ps, int* val)
{
if ((Empty(ps)))//判空
{
printf("队空,无法删除元素\n");
return -1;
}
*val = ps->data[(ps->rear - 1 + SIZE) % SIZE];
ps->rear = (ps->rear - 1 + SIZE) % SIZE;
ps->size--;
return 1;
}
//从队头插入元素
int push_head(queue* ps, int val)
{
if (isFull(ps))
{
printf("队满,无法加入元素\n");
return -1;
}
ps->front = (ps->front - 1 + SIZE) % SIZE;//先定位,再方便等下的插入
ps->data[ps->front] = val;
ps->size++;
return 1;
}
int main()
{
queue ps;
queueinit(&ps);
//测试队头插入数据
push_head(&ps, 1);
push_head(&ps, 2);
push_head(&ps, 3);
int val;
while (!Empty(&ps))
{
if (pop_rear(&ps, &val))
{
printf("%d", val);
}
}
return 0;
}