11

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.

Escualo
  • 40,844
  • 23
  • 87
  • 135

3 Answers3

4

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.

Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49
0

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:

Others on SO

Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 1
    Specifically, what problems are you referring to that can be made simpler with lists that contain themselves? – Justin L. Sep 16 '10 at 17:21
  • 1
    Thanks for the follow-ups/edits, but what does recursion with self-referencing lists offer that tail-end recursion and self-referencing *method* recursion cannot do and be more expressive at? – Justin L. Sep 16 '10 at 18:06
  • 1
    Recursion != Self reference – ruohola May 09 '19 at 08:43
0

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

Community
  • 1
  • 1
Rohan Monga
  • 1,759
  • 1
  • 19
  • 31