title: Traverse multi-linked list?
body: <p>I am creating a tool for a closed-source program. after debugging I have the base address of the structure I want to access. And after close inspection I see it's a multi-linked list, of where the first 3 integers are pointers, and all the other values are data.</p>
  
  <p>I have managed to reconstruct this structure from my analysis:</p>
  
  <pre><code>struct UnitsInfo
  {
      //pointers to UnitsInfo structures, null when no structure available at that location
      DWORD * a;//0x00
      DWORD * b;//0x04
      DWORD * c;//0x08
  
      //data
      int unkown_1;       //0x0C
      unsigned int Type;  //0x10
      unsigned int Amount;//ox14
  };
  UnitsInfo * Base_Node = ((UnitsInfo*)( (*(DWORD*)( (*(DWORD*)(0x00400000+0x008E98EC)) + 0x38)) + 0x0C);
  </code></pre>
  
  <p>Now, I am really new to linked-structures, let alone multi-linked structures.</p>
  
  <p>What I was thinking is making a map and bruteforcing until I have all adresses. (infinite loop possible)?</p>
  
  <p>But I know this is not the way to traverse this linked list. How could I efficiently, without knowing how it's connected, traverse all nodes in this multi linked list (and avoid the ones I already had)?</p>
  
  <hr>
  
  <p>Edit: Thanks to the answers I have finally made it!</p>
  
  <p>Here is my code, may come handy for other people?</p>
  
  <pre><code>#define MAX_PLAYERS (6)
  #define POINTER(type,addr) (*(type*)(addr))
  
  struct UnitsInfo
  {
      //pointers to UnitsInfo structures, null when no structure available at that location
      DWORD * nodes[3];//0x00
  
      //data
      int unkown_1;       //0x0C
      unsigned int Type;  //0x10
      unsigned int Amount;//ox14
  };
  
  struct pinfo
  {
      bool            in;
      enum Controller {Ctrl_UNKNOWN,  Ctrl_HUMAN, Ctrl_AI             };
      enum Nation     {N_UNKNOWN,     N_ALLIES,   N_SOVIET,   N_JAPAN };
      std::string     Name;
      Nation          Side;
      short           Team;
      Controller      Who;
      int             *Money;
      int             *Power;
      int             *Usage;
      unsigned int    *Color;
  
      bool            GotUnits;
      DWORD           *unit_start_node;
  };
  
  std::map&lt;DWORD*,UnitsInfo*&gt; UnitList[MAX_PLAYERS];
  void GenerateUnitList(unsigned short slot)
  {
      std::set&lt;DWORD*&gt; todo;
      unsigned int inserted = 1;
      while(inserted)
      {
          inserted = 0;
          for(auto it = UnitList[slot].begin(); it != UnitList[slot].end(); ++it)
          {
              for(short i = 0; i &lt; 3; ++i)
              {
                  if(it-&gt;second-&gt;nodes[i] != NULL)
                  {
                      if(UnitList[slot].find(it-&gt;second-&gt;nodes[i]) == UnitList[slot].end())
                      {
                          todo.insert(it-&gt;second-&gt;nodes[i]);
                          ++inserted;
                      }
                  }
              }
          }
          while(!todo.empty())
          {
              UnitList[slot][*todo.begin()] = &amp;POINTER(UnitsInfo,*todo.begin());
              todo.erase(todo.begin());
          }
      }
  }
  
  pinfo Player[MAX_PLAYERS];
  
  //adding the first node
  unsigned int CurrentSlot = (0xD8+(0x4*slot));
  if(POINTER(DWORD,POINTER(DWORD,0x00400000+0x008E98EC)+CurrentSlot) != NULL)
  {
      Player[slot].unit_start_node = &amp;POINTER(DWORD,POINTER(DWORD,POINTER(DWORD,POINTER(DWORD,0x00400000+0x008E98EC)+CurrentSlot) + 0x38) + 0x0C);
  }
  
  //adding first unit if available, if yes continue generating list
  if(!Player[i].GotUnits)
  {
      if(POINTER(DWORD,*Player[i].unit_start_node+0x10) != NULL)
      {
          Player[i].GotUnits = true;
          UnitList[i][(Player[i].unit_start_node)] = &amp;POINTER(UnitsInfo,*Player[i].unit_start_node);
      }
  }
  else
  {
      GenerateUnitList(i);
  }
  </code></pre>
  
  <p>And the best of all is, it works like a charm and doesn't lagg :)</p>