title: Keep track of location in an array of structs in c++
body: <p>For this assignment I'm using heaps to build a priority queue, sorted from lowest priority to highest. I created a structure which has two members, value and priority. In order to correctly locate each node's children, I need to keep track of its location. I originally had a third element in the struct, location, which was just set equal to heap_size, since nodes are added at the end of the array. </p>
  
  <pre><code>    heaptype Heap::getleft (heaptype *A, heaptype&amp; node)
      {
              heaptype lchild;
              lchild.location = 2*node.location;
  
              for(int i =0 ; i &lt;heapsize; i++)
              {
                      if(A[i].location == lchild.location)
                      {
                              lchild=A[i];
                      }
              return lchild;
              }
  
      void Heap::heapifyup(heaptype *A, heaptype &amp;node)
      {
              heaptype min;
              heaptype r = getright(A, node);
              heaptype l = getleft(A, node);
  
              if(l.location&lt;=heapsize &amp;&amp; l.priority&lt;node.priority)
              {
                      min = l;
              }
  
              else
              {
                      min = node;
              }
  
              if(r.location&lt;=heapsize &amp;&amp; r.priority&lt;l.priority)
              {
                      min = r;
              }
  
              if(min.priority!=node.priority)
              {
                      swap(min, node);
                      heapifyup(A, min);
              }
  
      }
  
      void Heap::swap (heaptype&amp; a, heaptype&amp; b)
      {
              heaptype temp;
              temp.id = a.id;
              a.id = b.id;
              b.id = temp.id;
              temp.priority = a.priority;
              a.priority = b.priority;
              b.priority = temp.priority;
      }
  </code></pre>
  
  <p>This program will correctly swap the id and priority of the nodes I send to it, but after I call the swap function the array I'm working with will assign both nodes to the value and priority of whatever the lowest priority node was. 
  Is there a way to fix the program I have now, or is there a more efficient way to keep track of the location of the nodes?         </p>