#include <iostream>
using namespace std;
const int MAXSIZE=20;? //設(shè)置隊(duì)列最大尺寸
struct Squeue? ? ? ? ? //隊(duì)列基本格式
{
? int data[20];
? int front;
? int rear;
};
void InitQueue(Squeue *t)? ? //隊(duì)列初始化
{
? ? t->front=0;
? ? t->rear=0;
}
int? QueueLength(Squeue *t)? //隊(duì)列實(shí)際大小計(jì)算
{
? ? return (t->rear-t->front+MAXSIZE)%MAXSIZE;
}
void EnQueue(Squeue *t)? ? //入隊(duì)操作满钟;只需要判斷隊(duì)列是否為隊(duì)滿(mǎn)狀態(tài)即可
{
? ? if((t->rear+1)%MAXSIZE==t->front)
? ? ? ? return ;
? ? else
? ? {
? ? ? ? cout<<"請(qǐng)輸入你將要入隊(duì)的元素:"<<endl;
? ? ? ? cin>>t->data[t->rear];
? ? ? ? t->rear=(t->rear+1)%MAXSIZE;
? ? }
}
void DeQueue(Squeue *t)? ? //出隊(duì),需要判斷隊(duì)列是否為空胳喷,因?yàn)闉榭毡悴豢蛇M(jìn)行出隊(duì)操作了湃番。
{
? ? if(t->rear==t->front)
? ? ? ? return;
? ? else
? ? {
? ? ? cout<<t->data[t->front]<<endl;
? ? ? t->front=(t->front+1)%MAXSIZE;? //取余的運(yùn)算實(shí)際上是在看該結(jié)果屬于哪個(gè)集合的哪個(gè)部分
? ? ? cout<<t->front<<endl;
? ? ? cout<<t->rear<<endl;
? ? }
}
int main()? ? ? ? ? ? ?
{
? ? Squeue A;
? ? Squeue *p=&A;
? ? InitQueue(p);
? ? int a=QueueLength(p);
? ? cout<<"隊(duì)列長(zhǎng)度為:"<<a<<endl;
? ? EnQueue(p);
? ? a=QueueLength(p);
? ? cout<<"隊(duì)列長(zhǎng)度為:"<<a<<endl;
? ? DeQueue(p);
? ? return 0;
}