1、图示
(1)、空队列
(2)、添加元素
(3)、添加元素
(4)、删除元素
2、代码
#define MAX_SIZE 255
typedef struct
{
int front;
int rear;
int maxSize;
int element[ MAX_SIZE] ;
} S_QUEUE;
void CreateQueue ( S_QUEUE* q, int maxsize)
{
q-> front = 0 ;
q-> rear = 0 ;
q-> maxSize = maxsize;
memset ( q-> element, 0 , MAX_SIZE * sizeof ( int ) ) ;
}
int IsEmpty ( S_QUEUE q)
{
return q. front == q. rear;
}
int IsFull ( S_QUEUE q)
{
return ( q. rear + 1 ) % q. maxSize == q. front;
}
void AddEle ( S_QUEUE* q, int ele)
{
if ( IsFull ( * q) )
return ;
q-> element[ ( q-> rear + 1 ) % q-> maxSize] = ele;
( q-> rear) ++ ;
}
void DelEle ( S_QUEUE* q, int * ele)
{
if ( IsEmpty ( * q) )
return ;
* ele = q-> element[ q-> front + 1 ] ;
( q-> front) ++ ;
}
void GetEle ( S_QUEUE* q, int * ele)
{
if ( IsEmpty ( * q) )
return ;
* ele = q-> element[ q-> front + 1 ] ;
}
int main ( )
{
S_QUEUE queue;
int i;
int tmp;
CreateQueue ( & queue, 10 ) ;
for ( i = 0 ; i < queue. maxSize; i++ )
{
AddEle ( & queue, i) ;
GetEle ( & queue, & tmp) ;
printf ( "tmp: %d front: %d rear: %d\n" , tmp, queue. front, queue. rear) ;
}
printf ( "\r\n" ) ;
for ( i = 0 ; i < queue. maxSize; i++ )
{
DelEle ( & queue, & tmp) ;
printf ( "tmp: %d front: %d rear: %d\n" , tmp, queue. front, queue. rear) ;
}
return 0 ;
}
3、输出