#include<stdio.h>
#include<stdlib.h>
typedef struct SQueue
{
int *arr;
int front;
int rear;
}SQueue;
//创建队列
void InitSQueue(SQueue *Q,int size)
{
Q = (SQueue *)malloc(sizeof(SQueue));
Q->arr = (int *)malloc(sizeof(size));
Q->front = 0;
Q->rear = 0;
}
//判断队满
bool isfull(SQueue *Q,int size)
{
if((Q->rear+1) % size == Q->front)
{
return true;
}
else
{
return false;
}
}
//判断队空
bool isEmpty(SQueue *Q,int size)
{
if(Q->front == Q->rear)
{
return true;
}
else
{
return false;
}
}
//入队
void EnQueue(SQueue *Q,int size,int x)
{
if(isfull(Q,size))
{
printf("队列已经满了,无法入队");
exit(0);
}
else
{
Q->arr[Q->rear] = x;
Q->rear = (Q->rear + 1) % size;
}
}
//出队
void DeQueue(SQueue *Q,int size,int &x)
{
if(isEmpty(Q,size))
{
printf("队内已经没有元素,无法出队");
}
else
{
x = Q->arr[Q->front];
Q->front = (Q->front + 1) % size;
}
}
//获取队头元素
void Gethead(SQueue *Q,int size,int &x)
{
x = Q->arr[Q->front];
}
int main()
{
SQueue *Q1;
InitSQueue(Q1,10);
EnQueue(Q1,10,5);
EnQueue(Q1,10,6);
int x;
DeQueue(Q1,10,x);
printf("出队元素:",x);
Gethead(Q1,10,x);
printf("队头元素:",x);
return 0;
}