循环队列 c 语言

#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;
}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百万攻城狮

你的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值