队列:特殊的线性表,“先进先出”,在尾结点处插入,头节点处删除。它也有有顺序存储和链式存储两种存储结构。
为了防止“假溢出”,顺序存储我们用循环队列来实现,为了便于辨别队列是否已满,通常要保证循环队列满的时候,数组仍有一个空闲空间。
判断循环队列:
队列已满:( rear + 1 ) % MAXIZE = front
队列为空:rear = front
队列长度:( MAXIZE - front + rear ) % MAXIZE
队列的结构定义:
typedef int Status ;
typedef struct
{
int *base ;
int front ;
int rear ;
} SqQueue ;
队列初始化:
void InitQueue( SqQueue *Q )
{
Q->base = ( int* )malloc( sizeof( int ) * MAXIZE ) ;
Q->front = Q->rear = 0 ;
}
队尾插入:
Status EnQueue( SqQueue *Q , int val )
{
if( ( Q->rear + 1 ) % MAXIZE == Q->front ) //用来保证队列满的时候,数组仍留出一个空闲空间
return ERROR ;
else
{
Q->base[ Q->rear ] = val ;
Q->rear = ( Q->rear +1 ) % MAXIZE ;
}
return OK ;
}
队头删除:
Status DeQueue( SqQueue *Q , int *val )
{
if( Q->front == Q->rear )
return ERROR ;
else
{
*val = Q->base[ Q->front ] ;
Q->front = ( Q->front + 1 ) % MAXIZE ;
return OK ;
}
}
遍历队列:
void TraverseQueue( SqQueue Q )
{
int i = Q.front ;
while( i != Q.rear )
{
printf( "%d " , Q.base[ i ] ) ;//也可以是其他操作
i = ( i + 1 ) % MAXIZE ;
}
printf( "\n" ) ;
}
取队头元素:
Status GetHead( SqQueue Q , int *e )
{
if( Q.front == Q.rear )
return ERROR ;
else
{
*e = Q.base[ Q.front ] ;
return OK ;
}
}
求队列的长度:
Status GetLength( SqQueue Q )
{
return ( MAXIZE - Q.front + Q.rear ) % MAXIZE ;
}
销毁队列:
void DesQueue( SqQueue *Q )
{
free( Q->base ) ;
Q->base = NULL ;
Q->front = Q->rear = 0 ;
}
源代码:
#include<stdio.h>
#include<stdlib.h>
#define MAXIZE 5
#define ERROR 0
#define OK 1
typedef int Status ;
typedef struct
{
int *base ;
int front ;
int rear ;
} SqQueue ;
void InitQueue( SqQueue *Q ) ;
Status EnQueue( SqQueue *Q , int val ) ;
Status DeQueue( SqQueue *Q , int *val ) ;
Status GetHead( SqQueue Q , int *e ) ;
Status GetLength( SqQueue Q ) ;
void TraverseQueue( SqQueue Q ) ;
void DesQueue( SqQueue *Q ) ;
int main( void )
{
int val , e ;
SqQueue Q ;
InitQueue( &Q ) ;
EnQueue( &Q , 3 ) ;
EnQueue( &Q , 4 ) ;
EnQueue( &Q , 5 ) ;
EnQueue( &Q , 6 ) ;
TraverseQueue( Q ) ;
DeQueue( &Q , &val ) ;
TraverseQueue( Q ) ;
printf( "被删除的元素是:%d\n" , val ) ;
EnQueue( &Q , 777 ) ;
TraverseQueue( Q ) ;
GetHead( Q , &e ) ;
printf( "现在队列的头元素是:%d\n" , e ) ;
printf( "现在队列的长度是:%d\n" , GetLength( Q ) ) ;
DesQueue( &Q ) ;
return 0 ;
}
void InitQueue( SqQueue *Q )
{
Q->base = ( int* )malloc( sizeof( int ) * MAXIZE ) ;
Q->front = Q->rear = 0 ;
}
Status EnQueue( SqQueue *Q , int val )
{
if( ( Q->rear + 1 ) % MAXIZE == Q->front ) //用来保证队列满的时候,数组仍留出一个空闲空间
return ERROR ;
else
{
Q->base[ Q->rear ] = val ;
Q->rear = ( Q->rear +1 ) % MAXIZE ;
}
return OK ;
}
Status DeQueue( SqQueue *Q , int *val )
{
if( Q->front == Q->rear )
return ERROR ;
else
{
*val = Q->base[ Q->front ] ;
Q->front = ( Q->front + 1 ) % MAXIZE ;
return OK ;
}
}
void TraverseQueue( SqQueue Q )
{
int i = Q.front ;
while( i != Q.rear )
{
printf( "%d " , Q.base[ i ] ) ;
i = ( i + 1 ) % MAXIZE ;
}
printf( "\n" ) ;
}
Status GetHead( SqQueue Q , int *e )
{
if( Q.front == Q.rear )
return ERROR ;
else
{
*e = Q.base[ Q.front ] ;
return OK ;
}
}
Status GetLength( SqQueue Q )
{
return ( MAXIZE - Q.front + Q.rear ) % MAXIZE ;
}
void DesQueue( SqQueue *Q )
{
free( Q->base ) ;
Q->base = NULL ;
Q->front = Q->rear = 0 ;
}