Circular Vector

Table of Contents

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
  • 只做了比较基本的功能
Share