I want to use a circular list.
Short of implementing my own (like this person did) what are my options?
Specifically what I want to do is iterate over a list of objects. When my iterator reaches the end of the list, it should automatically return to the beginning. (Yes, I realize this could be dangerous.)
See Vladimir's definition of a circular_iterator
: "A circular_iterator will never be equal with CircularList::end(), thus you can always dereference this iterator."
The C Programming language has many data structures like an array, stack, queue, linked list, tree, etc.
@cmaster-reinstatemonica -- from the perspective of the language definition, std::list is not a circular list. Its list of requirements does not include anything that makes it circular.
A circular linked list is a type of linked list in which the first and the last nodes are also connected to each other to form a circle. There are basically two types of circular linked list: 1. Circular Singly Linked List. Here, the address of the last node consists of the address of the first node.
To implement a circular singly linked list, we take an external pointer that points to the last node of the list. If we have a pointer last pointing to the last node, then last -> next will point to the first node. The pointer last points to node Z and last -> next points to node P.
There's no standard circular list.
However, there is a circular buffer in Boost, which might be helpful.
If you don't need anything fancy, you might consider just using a vector
and accessing the elements with an index. You can just mod
your index with the size of the vector to achieve much the same thing as a circular list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With