简体   繁体   English

使用 std::list 创建循环链表?

[英]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++ .所以,我一直想知道是否有一种方法可以在 c++ 中使用 std::list 创建循环链表 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. std::list 接口不支持循环列表,其中 first.prev 是指向 last 的指针,last.next 是指向 first 的指针。

You can't decrement the iterator returned by std::list::begin().您不能递减 std::list::begin() 返回的迭代器。 More generally, you can't decrement any iterator returned by std:: ... ::begin().更一般地说,您不能减少 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()).递增 std::list::back() 返回的迭代器会导致迭代器 == std::list::end() (通常对于 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.如果您很好奇,Visual Studio 模板在内部使用虚拟节点作为循环双向链表的一部分实现 std::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. dummy.next 指向第一个,dummy.prev 指向最后一个,first.prev == last.next == 指向虚拟节点的指针,但这不允许您将其视为传统的循环双向链表。

No, there is not.不,那里没有。

std::list does not allow you to control the links yourself. std::list不允许您自己控制链接。 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!例如,如果你遍历一个循环列表,你永远不会完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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