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

烂漫一生 提交于 2019-12-24 19:24:31

问题


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:


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;
    }


来源:https://stackoverflow.com/questions/10097583/how-can-i-iterate-over-a-quadruple-linked-2-dimensional-grid-of-data-as-if-it-we

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!