在顺序队列中,当队尾指针已经到数组的上界,不能再有入队操作,但其实数组中还有空位置,这就叫做“假溢出”,解决假溢出的途径---- 采用循环队列。
#include <iostream>
#include <fstream>
using namespace std;
#define max 100
#define ok 1
#define error 0
typedef struct
{
int *base;
int front;
int rear;
} squeel;
//初始化
int chushihua(squeel &q)
{
q.base = new int[max];
q.front = 0;
q.rear = 0;
return ok;
}
//求长度
int lengthl(squeel &q)
{
int l = q.rear - q.front + max;
{
return l % max;
}
}
//插入队列
int rudui(squeel &q, int e)
{
if ((q.rear + 1) % max == q.front)
{
return error;
}
q.base[q.rear] = e;
q.rear = (q.rear + 1) % max;
return ok;
}
int chudui(squeel &q, int &e)
{
if (q.front == q.rear)
{
return error;
}
e = q.base[q.front];
q.front = (q.front + 1) % max;
return ok;
}
int duitou(squeel q)
{
if (q.front != q.rear)
{
return q.base[q.front];
}
}
int main ()
{
squeel q;
int e, l, a;
chushihua(q);
rudui(q, 11);
rudui(q, 22);
rudui(q, 33);
l = lengthl(q);
cout << "长度" << l << endl;
a = duitou(q);
cout << "队头" << a << endl;
chudui(q, e);
cout << "第一个" << e << endl;
chudui(q, e);
cout << "第二个" << e << endl;
chudui(q, e);
cout << "第三个" << e << endl;
return 0;
}