How do I code a simple integer circular buffer in C/C++?

后端 未结 4 1281
长发绾君心
长发绾君心 2021-02-05 08:54

I see a lot of templates and complicated data structures for implementing a circular buffer.

How do I code a simple integer circular buffer for 5 numbers?

I\

4条回答
  •  忘了有多久
    2021-02-05 09:36

    int rI =0;
    int wI=0;
    #define FIFO_SIZE 3
    int checkAvail()
    {
    int avail=0;
    
    if(wI0)
    {
        a[wI]=val;
        wI++;
        if(wI>FIFO_SIZE)
            wI=0;
    }
    else
    {
        printf("FIFO full");
    }
    return 0;
    }
     int remFIFO(int *a)
     {
     int val;
    if((FIFO_SIZE-checkAvail()>0))
    {
        val =a[rI];
        rI++;
        if(rI>FIFO_SIZE)
            rI=0;
    }
    else
    {
        printf("FIFO empty");
    }
    return 0;
    }
    int main(array ^args)
    {
    int FIFO_ARRAY[FIFO_SIZE]={};
    addFIFO(FIFO_ARRAY,1);
    addFIFO(FIFO_ARRAY,2);
    addFIFO(FIFO_ARRAY,3);
    addFIFO(FIFO_ARRAY,4);
    remFIFO(FIFO_ARRAY);
    remFIFO(FIFO_ARRAY);
    remFIFO(FIFO_ARRAY);
    remFIFO(FIFO_ARRAY);
    remFIFO(FIFO_ARRAY);
    }
    

提交回复
热议问题