0

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,'#').

1 Answers1

1

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;
    }
cmspice
  • 318
  • 1
  • 6
  • Added an explanation of what I need above, I need x and y variables as well. – Molly Stewart-Gallus Apr 10 '12 at 23:18
  • I was debugging this and it almost works but doesn't stop upon reaching the bottom. I think I'll be able to fix it though. Edit: NVM. It's an error in my generating the grids. – Molly Stewart-Gallus Apr 10 '12 at 23:40
  • Ahah! I found the error. The small line of leftmost = process should be included after setting process = leftmost->below. I don't know the etiquette for this though. If I should wait for you to change the line and then I'd accept the post, if I should edit the post and place it in the editing queue or if to do something else. – Molly Stewart-Gallus Apr 11 '12 at 00:09