循环队列

typedef struct {
    int *base;    //存储基地址
    int front;    //队头
    int rear;     //队尾
    int size;     //元素个数
    int MAXSIZE;  //实际内存空间      
}SqQueue; 

int Queue_init(SqQueue *q,int size){

    q->base=(int *)malloc(sizeof(int)*size);
    if(q->base==NULL){ return -1; }
    q->MAXSIZE = size+1;
    q->front = q->rear = 0;
    q->size=0;
    return 0;
} 

int Queue_in(SqQueue *q,int e){
    
    if((q->rear+1) % q->MAXSIZE == q->front){printf("队列已满\n"); return -1;}
    q->base[q->rear] = e;
    q->rear = (q->rear+1) % q->MAXSIZE;
    q->size++;
    
    return 0;
}

int Queue_out(SqQueue *q){  
    
    if(q->front == q->rear){printf("队列已空\n");return -1;}
    int temp;
    temp=q->base[q->front];
    q->base[q->front] = 0;
    q->front=(q->front+1) % q->MAXSIZE;
    q->size--;
    return temp;
}
int Queue_empty(SqQueue *q){
    if(q->front == q->rear){printf("队列已空\n");return 0;}
}
int Queue_full(SqQueue *q){
    if((q->rear+1) % q->MAXSIZE == q->front){printf("队列已满\n"); return 0;}
}

你可能感兴趣的:(循环队列)