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

后端 未结 8 1673
心在旅途
心在旅途 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:05

    I don't think queue is the best way to make a cache. You want to be your cache to be really fast! And doing a linear scan of your queue is not the way to go unless you want your cache to be really small or your memory is really limited.

    Assuming that you don't want a very small cache or a slow cache, using a Linked List with a Hash Map of value to node in the linked list is a good way to go. You can always evict the head, and whenever an element is accessed, you can remove it and put it in the head of the list. For accessing you can directly get it or check if it's in the cache in O(1). Evicting an element is also O(1) and so is updating the list.

    For an example, look at LinkedHashMap in java.

    http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html

提交回复
热议问题