Circular vector

Code

struct circularVec{
    int vec[N];
    int front, rear;
    int cnt;

    void init(){
        memset(vec, 0, sizeof(vec));
        front = rear = cnt = 0;
    }

    bool fullQ(){return cnt == N;}

    bool emptyQ(){return cnt == 0;}

    void push(const int &x){
        if(cnt == N)return;
        ++cnt;
        if(front == rear && vec[front] == INF){
            vec[front] = x;
            return;
        }
        vec[rear = (rear + 1) % N] = x;
    }

    void pop(){
        if(!cnt)return;
        --cnt;
        vec[front] = INF;
        front = (front + 1) % N;
    }

    void printStatus(){
        for(int i = 0; i < N; ++i)printf("%d  ", vec[i]);printf("\n");
        printf("f = %d, r = %d, cnt = %d\n", front, rear, cnt);
    }
}v;

Review

  • 需要#include <cstring>
  • 需要const int Nconst int INF
  • 只做了比较基本的功能
posted @ 2021-10-08 10:43  Mojibake  阅读(58)  评论(0编辑  收藏  举报
知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。