编辑代码

#include <bits/stdc++.h>
using namespace std;
const int maxn=10;
int q[maxn];
int tail=0,head=0,length=0;
// 入队
void push(int i){
    length++;
    q[tail]=i;
    tail=(tail+1)%maxn;
}
// 出队
void pop(){
    length--;
    head=(head+1)%maxn;
}
// 判空
bool isEmpty(){
    // if(tail==head){
    //     cout<<"empty\n";
    //     return true;
    // }
    if(length==0){
        cout<<"empty\n";
        return true;
    }
    return false;
}
// 元素个数
int size(){
    // return (tail-head+maxn)%maxn;
    return length;
}
// 判满
bool isOverFlow(){
    // if((tail+1)%maxn==head){
    //     cout<<"overflow\n";
    //     return true;
    // }
    if(length==maxn){
        // cout<<head<<'\n'<<tail<<'\n';
        cout<<"overflow\n";
        return true;
    }
    return false;
}
// 打印
void print(){
    if(length==0)return ;
    if(head<tail){
        for(int i=head;i<tail;++i){
            cout<<q[i]<<' ';
        }
    }else {
        for(int i=head;i<tail+maxn;++i){
            cout<<q[i%maxn]<<' ';
        }
    }
}
int main() {
    for(int i=0;i<15;++i){
        if(isOverFlow())break;
        push(i+1);
    }
    for(int i=0;i<3;++i){
        if(isEmpty())break;
        pop();   
    }
    for(int i=0;i<3;++i){
        if(isOverFlow())break;
        push(i+1);
    }
    cout<<"元素个数:"<<size()<<'\n';
    print();
    cout<<'\n';
    // 获取队头
    // cout<<q[head];
	return 0;
}