How do I implement a circular list (ring buffer) in C?

后端 未结 8 1651
心在旅途
心在旅途 2020-12-02 08:40

How do I implement a circular list that overwrites the oldest entry when it\'s full?

For a little background, I want to use a circular list within GWT; so using a 3

8条回答
  •  攒了一身酷
    2020-12-02 09:21

    If you want a fixed length circular list. You can use a (dynamic) array. Use two variables for houskeeping. One for the position of the next element, one to count the number of elements.

    Put: put element on free place. move the position (modulo length). Add 1 to the count unless count equals the lengtht of the list. Get: only if count>0. move the position to the left (modulo length). Decrement the count.

提交回复
热议问题