Some ways to implement doubly circular linked lists

This time I want to share some(possibly dumb) ways to implement doubly circular linked lists, to incite you to think about that data structure in RGSS3.

Using Hash Directly

1. Building the list:

Spoiler
dcll = { head_node_id = [head_node_data, 2nd_node_id, tail_node_id], 2nd_node_id = [2nd_node_data, 3rd_node_id, 1st_node_id], 3rd_node_id = [3rd_node_data, 4th_node_id, 2nd_node_id], ... ith_node_id = [ith_node_data, i-1th_node_id, i+1th_node_id], ... tail_node_id = [tail_node_data, head_node_id, n-1th_node_id]}
head_node_id, tail_node_id and cur_node_id(the id of the currently pointed node in the list) should always be stored as long as the list exists to make using the list easier.

2. Accessing the current node data:

Spoiler
dcll[cur_node_id][0]
3. Going to the next i node:

Spoiler
i.times { cur_node_id = dcll[cur_node_id][1] }
4. Going to the previous i node:

Spoiler
i.times { cur_node_id = dcll[cur_node_id][2] }
5. Adding a new node as the next node of the current node:

Spoiler
next_node_id = dcll[cur_node_id][1]dcll[new_node_id] = [new_node_data, next_node_id, cur_node_id]dcll[cur_node_id][1] = dcll[next_node_id][2] = new_node_id
6. Removing the next node of the current node:

Spoiler
old_node_id = dcll[cur_node_id][1]next_node_id = dcll[old_node_id][1]dcll.delete(old_node_id)dcll[cur_node_id][1] = next_node_iddcll[next_node_id][2] = cur_node_id
7. Adding a new head node:

Spoiler
dcll[new_head_node_id] = [new_head_node_data, head_node_id, tail_node_id]dcll[tail_node_id][1] = dcll[head_node_id][2] = new_head_node_idhead_node_id = new_head_node_id
8. Adding a new tail node:

Spoiler
dcll[new_tail_node_id] = [new_tail_node_data, head_node_id, tail_node_id]dcll[tail_node_id][1] = dcll[head_node_id][2] = new_tail_node_idtail_node_id = new_tail_node_id
9. Removing the head node:

Spoiler
new_head_node_id = dcll[head_node_id][1]dcll.delete(head_node_id)head_node_id = new_head_node_iddcll[tail_node_id][1] = head_node_iddcll[head_node_id][2] = tail_node_id
10. Removing the tail node:

Spoiler
new_tail_node_id = dcll[tail_node_id][2]dcll.delete(tail_node_id)tail_node_id = new_tail_node_iddcll[tail_node_id][1] = head_node_iddcll[head_node_id][2] = tail_node_id
11. Shifting the head and tail node to their next i node:

Spoiler
i.times { head_node_id = dcll[head_node_id][1] }tail_node_id = dcll[head_node_id][2]
12. Shifting the head and tail node to their previous i node:

Spoiler
i.times { head_node_id = dcll[head_node_id][2] }tail_node_id = dcll[head_node_id][2]
P.S.: Using dcll.keys to do the above would likely lead to much, much more performant solutions XD

Using Variable Pointers

DoubleX RMVXA Variable Pointers Tutorial

It's incredibly alien, complicated and idiotic, making it an excellent counter example of implementing doubly circular linked lists lol

What are your opinions on doubly circular linked lists? Do you think they're useful in RGSS3 scripts? How would you implement them? Would you implement some other data structures using them?

P.S.S.: Normally, each node in a doubly circular linked list has a data, next and previous pointer(storing the memory address of another variable) pointing to the next and previous node. If that pointer behavior needs to be replicated as well, both the aforementioned ways will fail. Maybe using Fiddle:😛ointer can do the job(I wonder if RGSS3 supports that though).
Normally, each node in a doubly circular linked list has a data, next and previous pointer(storing the memory address of another variable) pointing to the next and previous node.
You might plan this a bit overcomplicated here. Every variable holding an object in Ruby is actually a memory pointer.
You might plan this a bit overcomplicated here. Every variable holding an object in Ruby is actually a memory pointer.
Yeah, but some might want to access those memory address values themselves. I've heard some complaining RGSS3 has no pointer arithmetic(although it's not about doubly circular linked list, and I doubt if the latter needs to use the former) 🙂
I'm not that experienced with using pointers, but if you really need multiple variables to always share the same object reference for example, you could wrap that reference in another object:

Reference = Struct.new🙂value)p1 = Reference.new(19) # New Referencep2 = Reference.new(19) # New Referencep3 = p2 # Share Reference with 'p2'p p1.value, p2.value, p3.value  # => 19, 19, 19p2.value += 1p p1.value, p2.value, p3.value  # => 19, 20, 20You probably wouldn't have needed a hash to store your list nodes unless you want to access them randomly. Instead of a nodes ID, you could also refer to the node object directly.

Original thread: https://forums.rpgmakerweb.com/threads/some-ways-to-implement-doubly-circular-linked-lists.40802/