#include<iostream>
#include<string>
using namespace std;
#define MAXSIZE 100
typedef struct
{
int data[MAXSIZE];
int front;
int rear;
}Seqqueue, * PSeqqueue;
//初始化队
PSeqqueue Init_SeqQueue(void)
{
PSeqqueue Q = (PSeqqueue)malloc(sizeof(Seqqueue));
if (!Q)
return 0;
Q->front = 0;
Q->rear = 0;
return Q;
}
//入队
//由于队头一直向后退,会造成假溢出,所以我们采用循环队列
// //所以随着入队,出队的进行,导致队头和队尾转了好多圈,简单的说就是队头和队尾已经大于MAXSIZE(队的最大容量)
//所以要对MAXSIZE取余.头指针不放元素,从front的下一位置放元素。
int Push_SeqQueue(PSeqqueue Q, int e)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)//由于队头一直向后退,会造成假溢出,所以我们采用循环队列
//所以随着入队,出队的进行,导致队头和队尾转了好多圈,简单的说就是队头和队尾已经大于MAXSIZE(队的最大容量)
//所以要对MAXSIZE取余.头指针不放元素,从front的下一位置放元素。
{
cout << "队满";
return 0;
}
Q->rear = (Q->rear + 1) % MAXSIZE;
Q->data[Q->rear] = e;
return 1;
}//出队
int Pop_SeqQueue(PSeqqueue Q, int* e)
{
if (Q->rear == Q->front)//显然队头和对位肯定在同一周期,所以不需要对MAXSIZE取余
{
cout << "队空";
return 0;
}
Q->front = (Q->front + 1) % MAXSIZE;
*e = Q->data[Q->front];
return 1;
}
//判空
int Empty_SeqQueue(PSeqqueue Q)
{
if (Q->front == Q->rear)
{
return 1;
}
return 0;
}
//取队头元素
int Get_top_SeqQueue(PSeqqueue Q, int x)
{
if (Q->front == Q->rear)
{
cout << "队空";
return 0;
}
x = Q->data[(Q->front + 1) % MAXSIZE];
return 1;
}
//销毁队
int Destroy_SeqQueue(PSeqqueue* Q)
{
if (*Q)
{
free(*Q);
*Q = NULL;
}
return 1;
}
int main()
{
PSeqqueue Q;
int e = 0;
Q = Init_SeqQueue();
Push_SeqQueue(Q, 1);
Pop_SeqQueue(Q, &e);
cout << '\n' << e << endl;
Destroy_SeqQueue(&Q);
}