#include <stdio.h>
#include <stdlib.h>
#define CLEN 5
typedef struct
{
int cap; //队列的长度
int front; //队列头部
int rear; //队列尾部
int array[CLEN];
}RECQUEUE;
enum Flag
{
ERROR = -1 ,
OK
};
int isEmpty( RECQUEUE queue )
{
if( queue.front == queue.rear )
{
return OK;
}
return ERROR;
}
int isFull( RECQUEUE queue )
{
if( ( queue.rear + 1 ) % queue.cap == queue.front )
{
return OK ;
}
return ERROR ;
}
int inQueue( RECQUEUE *queue , int value )
{
if( OK == isFull( *queue ) )
{
return ERROR;
}
queue->array[queue->rear] = value;
queue->rear = ( queue->rear + 1 ) % queue->cap;
return OK ;
}
int deQueue( RECQUEUE *queue , int *value )
{
if( OK == isEmpty( *queue ) )
{
printf( "执行了\n" );
return ERROR;
}
*value = queue->array[queue->front];
queue->front = ( queue->front + 1 ) % queue->cap ;
return OK;
}
int showQueue( RECQUEUE queue )
{
if( OK == isEmpty( queue ) )
{
return ERROR;
}
int cFront = queue.front;
int cRear = queue.rear;
int value ,
ret ;
while( OK == deQueue( &queue , &value ) )
{
printf( "执行了\n" );
printf( "%d " , value );
}
return OK ;
}
int main(int argc, char *argv[])
{
RECQUEUE queue;
queue.cap = sizeof( queue.array ) / sizeof( int ) ;
queue.front = 0 ;
queue.rear = 0 ;
int ret;
ret = inQueue( &queue , 2 );
printf( "%d\n" , ret );
inQueue( &queue , 3 );
inQueue( &queue , 4 );
inQueue( &queue , 5 );
ret = inQueue( &queue , 6 );
printf( "%d\n" , ret );
showQueue( queue );
printf( "%d\n" , ret );
system("PAUSE");
return 0;
}
结果: