One of the most classic examples of a cyclic structure can be seen in the concept of a doubly-linked list. In a doubly-linked list, each element is connected to both the previous and next elements in the list, creating a cycle of connections within the data structure.
X --> Y --> Z
X <-- Y <-- Z
(Even though X, Y, and Z are repeated here, they represent individual objects.)
For example, if X points to Y as the next element, then Y will point back to X as the previous element, forming a cycle between X and Y. This same pattern exists for every element in the list, with middle elements belonging to two separate cycles.
To avoid the complexity of serializing lists with these types of cyclic structures, one solution is to assign unique IDs to each object. By representing elements using IDs instead of direct pointers, we eliminate the cyclical relationships present in the original structure. For instance:
list: {
items:
{ id:"alpha", value1: ... etc },
{ id:"beta", values .... },
{ id:"gamma", values .... }
links:
{ elem:"alpha", next:"beta" },
{ elem:"beta", next:"gamma", prev: "alpha"},
{ elem:"gamma", prev:"beta" }
}