全部博文(776)
分类: C/C++
2010-11-30 08:31:48
#include
#include
#define MAXSIZE 10
typedef struct CQNode
{
int data[MAXSIZE];
int front;
int end;
} CQNode, *CQList;
void InitCQL ( CQList CQ )
{
if ( CQ == NULL )
{
CQ = ( CQList ) malloc ( sizeof ( CQNode ) );
}
CQ->front = 0;
CQ->end = 0;
}
void InsertCQL ( CQList CQ, int value )
{
if ( ( CQ->end - CQ->front + MAXSIZE + 1 ) % ( MAXSIZE ) == 0 )
{
printf ( "Queue is full" );
printf ( "%d is not insert\n", value );
return;
}
else
{
CQ->data[CQ->end] = value;
CQ->end++;
CQ->end = CQ->end % MAXSIZE;
}
}
int DeleteCQL ( CQList CQ )
{
int res;
if ( cq->end == cq->front )
{
printf ( "queue is empty\n" );
return -1;
}
else
{
res = CQ->data[CQ->front];
CQ->front++;
CQ->front = CQ->front % MAXSIZE;
}
return res;
}
void Print ( CQList CQ )
{
int i;
i = CQ->front;
while ( i != CQ->end )
{
printf ( "%d,", CQ->data[i] );
i++;
if ( i == MAXSIZE )
i = 0;
}
printf ( "\n" );
}
int main()
{
int i, val;
CQList cl;
cl = ( CQList ) malloc ( sizeof ( CQNode ) );
InitCQL ( cl );
for ( i = 1; i < 12; i++ )
{
InsertCQL ( cl, i * 3 + 3 );
}
Print ( cl );
getchar();
for ( i = 1; i < 7; i++ )
{
val = DeleteCQL ( cl );
if ( val != -1 )
printf ( "%d is deleted\n", val );
}
getchar();
for ( i = 4; i < 12; i++ )
{
InsertCQL ( cl, i * 6 );
}
Print ( cl );
getchar();
return 0;
}