How can I iterate over a quadruple linked 2-dimensional grid of data as if it were a 2-dimensional array?

StackOverflow https://stackoverflow.com/questions/10097583

Question

How can I iterate over a quadruple linked 2-dimensional grid of data as if it were a 2-dimensional array?

My grid structure is:

typedef bool tile;

struct BLOCK;
typedef struct BLOCK block;

struct BLOCK {
 const block * to_the_left;
 const block * above;
 const block * to_the_right;
 const block * below;

 tile data;
};

typedef struct {
 const block * start;
} map;

I need to be able to iterate over this grid like it is a 2-dimensional array so I can display tiles of the map on the screen centering around the start block.

P.S.S. I would most preferable like to see a solution in C, (this is what I'm coding in for this project), C++, Haskell, or Java code as those are languages I know well, but any language is fine. I just need the algorithm.

P.S.S.S. For clarity, by iterate over like a 2-dimensional array I mean I need to get an index into the x and y position as variables. For example, I need to do call mvaddch(y,x,'#').

Was it helpful?

Solution

this will iterate in the same order as if it were a 2 dimensional array

    BLOCK * process = upper_left_block;
    BLOCK * leftmost = process;
    while(true)
    {
        //<do stuff here>
        process = process->to_the_right;
        if(process == null)
        {
            process = leftmost->below;
            leftmost = process;
        }
        if(process == null)
            break;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top