Uses of self referencing lists

可紊 提交于 2019-11-27 02:45:28

问题


I know it is possible to create a self referencing list in languages like Python:

>>> my_list = [1,2]
>>> my_list.append(my_list)
>>> print my_list
[1,2,[...]]
>>> print my_list[0]
1
>>> print my_list[2]
[1,2,[...]]

What algorithms benefit from self referencing lists? I cannot think of one.

Thanks.


回答1:


Self-referencing lists, and, generally speaking, circular data structures, can be caused when representing a graph using data structures.

For example, consider this naive representation of a graph: Each node is either an atomic value, or a list of nodes that it is linked to. A circle may cause a list to contain another list that contains the list. A self-circle, i.e., an edge from a node to itself, will cause a self-referencing list.




回答2:


If you are asking just about lists, then I can't think of something right now, except for maybe recursively creating/searching in a data structure modeled as list.

But one application of a self-referencing could be this Self Referencing Class Definition in python




回答3:


Most recursive problem definition uses some kind of self refrential objects or a data with self-referential definition.

I would add the wikipedia link as it provides a good readup:

  • http://en.wikipedia.org/wiki/Recursion_(computer_science)

Others on SO

  • What is recursion and when should I use it?
  • Recursive Sets vs Recursive Functions
  • Understanding recursion
  • Recursive Splay Tree
  • https://stackoverflow.com/questions/2085834/how-did-you-practically-use-recursion


来源:https://stackoverflow.com/questions/3728667/uses-of-self-referencing-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!