简体   繁体   中英

Create circular linked list using std::list?

So, I've been wondering if there is a way to create a circular linked list with std::list in c++ . I can't seem to figure out a way to get the last element to point to the first. Any ideas how someone could do that?

std::list interface doesn't support a circular list where first.prev is a pointer to last, and last.next is a pointer to first.

You can't decrement the iterator returned by std::list::begin(). More generally, you can't decrement any iterator returned by std:: ... ::begin().

Incrementing the iterator returned by std::list::back() results in an iterator == std::list::end() (the same is true in general for std:: ... :: back()).


If you're curious, Visual Studio template implements std::list internally using a dummy node as part of a circular doubly linked list. dummy.next points to first, dummy.prev points to last, and first.prev == last.next == pointer to dummy node, but this doesn't allow you to treat this as a conventional circular doubly linked list.

No, there is not.

std::list does not allow you to control the links yourself. The list controls the links for you. You cannot customize them.

Note that if you could customize the links in the list, the functions in the list would probably not work correctly. For example, if you iterated over a circular list, you'd never finish!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM