title: Making an adjacency list in C++ for a directed graph
body: <p>Hello all :)  Today I am refining my skills on graph theory and data structures.  I decided to do a small project in C++ because it's been a while since I've worked in C++.  </p>
  
  <p>I want to make an adjacency list for a directed graph.  In other words, something which looks like:</p>
  
  <pre><code>0--&gt;1--&gt;3
  1--&gt;2
  2--&gt;4
  3--&gt;
  4--&gt;
  </code></pre>
  
  <p>This would be a directed graph with V0 (vertex 0) having an edge to V1 and V3, V1 having an edge to V2, and V2 having an edge to V4, like this:</p>
  
  <pre><code>V0-----&gt;V1----&gt;V2----&gt;V4
   |
   |
   v
   V3 
  </code></pre>
  
  <p><strong>I know that in order to do this, I will need to create an adjacency list in C++.</strong>  An adjacency list is basically an <strong>array of linked lists</strong>.  Okay, let's see some pseudo C++ code:</p>
  
  <pre><code>#include &lt;stdio&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  struct graph{
  //The graph is essentially an array of the adjList struct.  
  node* List[];
  
  };
  
  struct adjList{
  //A simple linked list which can contain an int at each node in the list.
  
  };
  
  struct node {
  int vertex;
  node* next;
  };
  
  int main() {
  //insert cool graph theory sorting algorithm here
  }
  </code></pre>
  
  <p>As you can tell, this pseudocode is currently far from the mark.  And that is what i wanted some help with -- pointers and structs in C++ have never been my strong suit.  First of all, this takes care of the vertices that a vertex points to -- but what about the vertex itself?  How can I keep track of that vertex?  When I loop over the array, it will do me no good to only know what vertices are being pointed to, rather than also knowing what points <em>to</em> them.  The first element in each list should probably be that vertex, and then the elements after that are the vertices it points to.  But then, how can I access this first element of the list in my main program?  (sorry if this is convoluted or confusing, I would happy to rephrase).   </p>
  
  <p>I would like to be able to loop over this adjacency list to do some cool things with graphs.  For example, to implement some graph theory algorithms (sorts, shortest paths, etc) using the adjacency list representation.</p>
  
  <p>(Also, I had a question about the adjacency list.  What is different than just using a list of arrays?  Why can't I just have a list with an array at each element in the list?)</p>