练习题
//代码:能处理上溢和下溢的队列
//入队列
void Enqueue2(queue &Q, int x)
{
int t;
//上溢
if(Q.tail == Q.length)
t = 1;
else t= Q.tail+1;
if(t == Q.head)
{
cout<<"error:overflow"<<endl;
return;
}
else
{
Q.s[Q.tail] = x;
Q.tail = t;
}
}
//出队列
int Dequeue2(queue &Q)
{
//下溢
if(Q.head == Q.tail)
{
cout<<"error:underflow"<<endl;
return -1;
}
int x = Q.s[Q.head];
if(Q.head == Q.length)
Q.head = 1;
else Q.head++;
return x;
}Last updated