循环队列(用顺序表来实现)

来源:互联网 发布:知乎app下载安卓 编辑:程序博客网 时间:2024/06/05 13:21
//怎样使顺序循环队列不损失一个空间,并且又能判断队列已经满了呢??
//只要多设计一个标志tag  当tag=0时表示出队, 当tag=1时表示入队
//队列为空  front==rear && tag==0
//队列为满  front==rear && tag==1

///////////////////////////////////////////////////////////head.h//////////////////////////////////////////
#include <iostream>
using namespace std;

#define queueSize 10

struct SQueue
{
char queue[queueSize];
int front , rear;
int tag;
};
///////////////////////////////////////////////////head.cpp//////////////////////////////////////////////////
#include "head.h"

void initQueue(SQueue &Q)
{
Q.front=Q.rear=0;
Q.tag=0;///////////////初始化标志为0
}

bool queueEmpty(SQueue Q)
{
if( Q.front==Q.rear && Q.tag==0 )
{
cout<<"队列为空"<<endl;
return true;
}
else
{
cout<<"队列不为空"<<endl;
return false;
}
}

bool enterQueue(SQueue &Q, char e)
{
if(Q.rear>=queueSize || (Q.front==Q.rear   &&  Q.tag==1) )
{
cout<<"队列已满,不能进队"<<endl;
return false;
}
else
{
Q.queue[Q.rear]=e;
Q.rear++;
Q.tag=1;

return true;
}
}

void getHead(SQueue Q)
{
cout<<"第一个元素为:  ";
cout<<Q.queue[Q.front]<<endl;
}

bool deleteQueue(SQueue &Q)
{
if( Q.front==Q.rear  && Q.tag==0)
{
cout<<"空的队列不能进行删除"<<endl;
return false;
}
else
{
Q.front=Q.front+1;
Q.tag=0;
return true;
}
}

上面可能有错误,请多多指教!!!