#include<iostream.h>
#include<stdlib.h>
const int maxsize=50;
typedef int elemtype;
struct queue
{
elemtype queue[maxsize];
int front,rear;
};
void initqueue ( queue &q )
{
q.front=q.rear=0;
}
void clearqueue ( queue &q )
{
q.front=q.rear=0;
}
int queueempty ( queue &q )
{
return q.front==q.rear;
}
elemtype qfront ( queue &q )
{
if ( q.front==q.rear )
{
cerr<<"queue is empty!"<<endl;
exit ( 1 );
}
return q.queue[ ( q.front+1 ) %maxsize];
}
void qinsert ( queue &q,elemtype x )
{
int k= ( q.rear+1 ) %maxsize;
if ( k==q.front )
{
cerr<<"queue overflowe!"<<endl;
exit ( 1 );
}
q.rear=k;
q.queue[k]=x;
}
elemtype qdelete ( queue &q )
{
if ( q.front==q.rear )
{
cerr<<"queue is empty!"<<endl;
exit ( 1 );
}
q.front= ( q.front+1 ) %maxsize;
return q.queue[q.front];
}
int queuefull ( queue &q )
{
return ( q.rear+1 ) %maxsize==q.front;
}
int queuesize ( queue &q )
{
return ( q.rear-q.front ) %maxsize;
}
void lookqueue ( queue &q )
{
if ( q.front==q.rear )
{
cerr<<"queue is empty!"<<endl;
exit ( 1 );
}
int k= ( q.front+1 ) %maxsize;
while ( 1 )
{
cout<<q.queue[k]<<" ";
if ( k==q.rear )
break;
k= ( k+1 ) %maxsize;
}
cout<<endl;
}
void main()
{
queue q;
initqueue ( q );
for ( int i=0; i<6; i++ )
{
int x=rand() %100;
int y=rand() %100;
if ( !queuefull ( q ) )
{
qinsert ( q,x );
cout<<x<<" in queue, " ;
}
if ( !queuefull ( q ) )
{
qinsert ( q,y );
cout<<y<<" in queue" ;
}
cout<<endl;
cout<<"queuesize is: "<<queuesize ( q ) <<endl;
cout<<qdelete ( q ) <<" out queue"<<endl;
cout<<"queuesize is: "
<<queuesize ( q ) <<endl;
}
cout<<endl;
lookqueue ( q );
while ( !queueempty ( q ) )
{
cout<<qdelete ( q ) <<endl;
cout<<"queuesize is: "
<<queuesize ( q ) <<endl;
}
}