关键在于::
数组形式
与
循环队列
//顺序存储结构
#include
#include
#include
const int MAXSIZE=10;
typedef int ElemType;
struct SeQueuestr
{
ElemType elem[MAXSIZE];
int front, rear;
};
using namespace std;
class SeQueue
{
public:
SeQueue();
~SeQueue();
int Empty();
void Display();
void AddQ(ElemType x);
ElemType DelQ();
private:
ElemType elem[MAXSIZE];
int front, rear;
};
SeQueue::SeQueue()
{
front = 0;
rear = 0;
}
SeQueue::~SeQueue()
{
}
int SeQueue::Empty() {
if (rear=front)
{
return 1;
}
else
{
return 0;
}
}
void SeQueue::Display() {
cout << "开始输出" << endl;
int f;
f = front;
while (f!=rear)
{
cout << elem[f+1]<< endl;
f = (f + 1) % MAXSIZE;
}
cout << "输出完毕" << endl;
}
void SeQueue::AddQ(ElemType x) {
if ((rear+1)%MAXSIZE==front)//循环队列的满队列判断
{
cout << "Queue is Full!!" << endl;
}
else
{
rear = (rear + 1) % MAXSIZE;//出队列或者进队列都要头尾指针做加一后取余运算;
elem[rear] = x;
}
}
ElemType SeQueue::DelQ() {
if (rear==front)//循环队列的空判断
{
cout << "Queue is empty!" << endl;
return -1;
}
else
{
front = (front + 1) % MAXSIZE;
return elem[front];
}
}
int main() {
ElemType e;
int a,j;
SeQueue s;
cout << "队列顺序存储结构" << endl;
cout << "输入1进行入队,输入2进行出队,输入-999结束" << endl;
cin >> a;
while (a != -999)
{
if (a == 1) {
cout << "输入入队数据:" << endl;
cin >> e;
s.AddQ(e);
s.Display();
cout << "输入指令:" << endl;
cin >> a;
}
if (a == 2) {
e = s.DelQ();
cout << "出队数据为:" << e << endl;
s.Display();
cout << "输入指令:" << endl;
cin >> a;
}
else {
cout << "错误指令!" << endl;
cout << "输入指令:" << endl;
cin >> a;
}
}
_getch();
return 0;
}