2022.2.9
循环队及其基本操作
(下一节 栈)
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
#define ElemType int 
typedef struct Queue{
    ElemType *data;
    int head,rear;  
    int length,size;
}Queue;
//初始化
Queue * Init(int n){
    Queue *p=(Queue *)malloc(sizeof(Queue));
    p->head=0,p->rear=0,p->size=0,p->length=n;
    p->data=(ElemType *)malloc(sizeof(ElemType)*n);
    return p;
}
//判空
bool Empty(Queue *p){
    if(p->size==0)
        return true;
    else
        return false;
}
//返回队首元素
ElemType Front(Queue *p){
    if(p==NULL||p->size==0)return false;
    return p->data[p->head]; 
}
//val入队
bool push(Queue *p,int val){
    if(p==NULL)return false;
    if(p->size==p->length)return false;
    p->data[p->rear]=val;
    p->rear=(p->rear+1)%(p->length);
    p->size++;
    return true;
}
//出队
bool pop(Queue *p){
    if(p==NULL)return false;
    if(p->size==0)return false;
    p->head= (p->head+1)%(p->length);
    p->size--;
    return true;
}
//销毁队
bool Clear(Queue *p){
    if(p==NULL)return false;
    free(p->data);
    free(p);
    return true;
}
//输出队列元素
bool Output(Queue *p){
    printf("[-");
    for(int i=p->head,j=0;j<p->size;j++){
        printf("%d-",p->data[i]);
        i=(i+1)%(p->length);
    }
    printf("]\n");
}
int main(){
    Queue *p=Init(100);
    for(int i=0;i<55;i++){
        push(p,i);
    }
    Output(p);
    pop(p);
    Output(p);
    Clear(p);
    Output(p);
    return 0;
} 
测试正确











