I am working on a Swift implementation of dancing links algorithm to solve exactCover problems.
I need a linked list (linked matrix actually) that can be navigated in 2-D (left/right, up/down). I am mostly there using the LinkedList class / tutorial proved here - thank you very much.
However, I’m fairly new to Swift and could use some assistance with one final step. I need a way to modify the Collection implementation, or create a new one to return the vertical list like the horizontal one it currently returns. I’d be happy to provide the changes I’ve made to the code, but this forum seems to be the only contact option and I don’t see the ability to upload a file.
My changes are fairly straight-forward:
-
For my own sanity, I changed next/prev to right/left
-
I added up/down links
-
I added the ability for the various iterators to work with circular links
-
I added SetHead() to initialize a list with the same head as another one because it’s handy to have reference to the list from both directions.
-
I renamed append(Node) to appendHorizontal(Node)
-
I added appendVertical(Node)
-
I modified append() to accept an optional parameter for up/down
-
I added lastVertical()
It all works and my sparse matrix looks good, but navigating it vertically is cumbersome. So, I’d like to be able to do something like this:
In addition to Navigate Horizontally (as it originally worked):
for hNode in myLinkedList { … }
I’d like to be able to - not sure how to do this
for vNode in myLinkedList(vertical: true) { … } // or something similar
Thanks in advance!