title: Towers of Hanoi C++ Implementation with Stacks
body: <p>I've written the following code for a recursive ToH algorithm with stacks and can't figure out why it fails. There are no compilation errors, but once the program actually starts to run, it "thinks" a little bit then crashes. Any ideas?</p>
  
  <p>Relevant code:</p>
  
  <pre><code>void algoritmoDeTorres(int numDiscos, pila &amp;origen, pila &amp;aux, pila &amp;meta)
  {
      GotoXY(0,0);
      if(numDiscos==1)
      {
          int item;
          item = origen.pop(); //crashes in this function
          lista laux;
          laux.insertaInicio(item);
          meta.push(item);
          return;
      }
      algoritmoDeTorres(numDiscos - 1, origen, aux, meta);
      origen.imprimePila();
      cout &lt;&lt; endl;
      aux.imprimePila();
      cout &lt;&lt; endl;
      meta.imprimePila();
      algoritmoDeTorres(numDiscos -1, aux, meta, origen);
  }
  
  
  class pila
  {
      private:
          lista lisst;
      public:
          int pop()
          {
              int tam;
              tam = lisst.regresaItem();
              lisst.borraInicio();
              return tam;
          }        
      };
  
  class lista
  {
  private:
      nodo *cabeza;
  public:
      lista()
      {
          cabeza = NULL;
      }
      void borraInicio()
      {
          nodo * aux;
          aux = cabeza-&gt;next; 
          delete cabeza;
          cabeza = aux;
      }
      int regresaItem()
      {
          return cabeza-&gt;item; //crashes here specifically
      }
  };
  
  class nodo
  {
  public:
      int item;
      nodo* next;
  
      nodo(int a,nodo * siguiente)
      {
          item = a;
          next = siguiente;
      }
  };
  
  int main()
  {
      pila ORIGEN,AUX,META;
      ORIGEN.push(3);
      ORIGEN.push(2);
      ORIGEN.push(1);
  
      algoritmoDeTorres(ORIGEN.Size(),ORIGEN,AUX,META);
  
      ORIGEN.destruirPila();
      AUX.destruirPila();
      META.destruirPila();
      return 0;
  }
  </code></pre>
  
  <p>PS: Sorry for the Spanglish, my class is in Spanish but many of the ideas are still presented in English, hence the funky language. </p>
  
  <p>Important translations should they be necessary:</p>
  
  <p>aloritmoDeTorres - Towers Algorithm</p>
  
  <p>Discos - Disks</p>
  
  <p>pila - Stack</p>
  
  <p>Origen - Origin</p>
  
  <p>Meta - Destination/Goal</p>
  
  <p>InsertaInicio - Insert(at)Beginning</p>
  
  <p>Imprime - Print</p>
  
  <p>Regresa - Return</p>
  
  <p>Borra - Erase/Delete</p>
  
  <p>Nodo - Node</p>
  
  <p>Cabeza - Head</p>
  
  <p>Siguiente - Next</p>