Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a standard implementation of a Circular List exist for C++?

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."

like image 525
Runcible Avatar asked Jun 03 '09 21:06

Runcible


People also ask

Does C have built in linked list?

The C Programming language has many data structures like an array, stack, queue, linked list, tree, etc.

Is STD list Circular?

@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.

What is a circular linked list in C explain with the help of examples?

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.

How are circular linked list implemented?

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.


1 Answers

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.

like image 56
Naaff Avatar answered Oct 06 '22 01:10

Naaff