title: How to delete a user-specified data node in a linked list?
body: <p>In the function <em>void eliminarXdato()</em> the user will be asked to write a number and if that number is the same as one found in the linked list structure <em><strong>cedula</strong></em> it will delete them. The problem is probably logical, it just crashes and abruptly ends the program whenever i try to initialize said function so i dont get any clues of why.</p>
  <p>I tried to omit unnused functions.</p>
  <p><code>warning: ‘cab’ may be used uninitialized in this function</code></p>
  <p>cab intends to act as head.</p>
  <p>Code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  #include &lt;windows.h&gt;
  
  using namespace std;
  
  void menu();
  void ingresar();
  
  // void mostrar();
  // void eliminarInicio();
  // void eliminarCentral();
  // void eliminarFinal();
  
  void eliminarXdato();
  
  // void funcionLeccion();
  
  struct nodo
  {
      int cedula; // cedula aka data
      struct nodo* nxt;
  };
  
  typedef struct nodo* lista;
  
  lista cab, aux, aux1;
  int opc = 0;
  
  /*
  int contador(struct nodo* cab)
  {
      int cont = 0;
      while (cab != NULL) {
          cab = cab-&gt;nxt;
          cont++;
      }
      return cont;
  }
  */
  
  int main()
  {
      menu();
      return 0;
  }
  
  void menu()
  {
      system(&quot;cls&quot;);
      do
      {
          printf(&quot;MENU PRINCIPAL\n\n&quot;);
          printf(&quot;1. Ingresar nuevos datos\n&quot;);
          printf(&quot;2. Mostrar la lista\n&quot;);
          printf(&quot;3. Eliminar el primer dato de la lista (Mas antiguo)\n&quot;);
          printf(&quot;4. Eliminar el dato central de la lista\n&quot;);
          printf(&quot;5. Eliminar el ultimo dato de la lista (Mas nuevo)\n&quot;);
          printf(&quot;6. Eliminar dato el deseado de la lista\n&quot;);
          printf(&quot;7. Terminar el proceso\n&quot;);
          cin &gt;&gt; opc;
  
          switch (opc)
          {
              case 1:
                  ingresar();
                  break;
              case 2:
                  mostrar();
                  break;
  
  /*          case 3: eliminarInicio();
                  break;
              case 4: eliminarCentral();
                  break;
              case 5: eliminarFinal();
                  break;
  */
              case 6:
                  eliminarXdato(); // function
                  break;
              default:
                  break;
          }
      } while (opc != 7);
  }
  
  void ingresar()
  {
      do
      {
          system(&quot;cls&quot;);
          aux = new (struct nodo);
  
          cout &lt;&lt; &quot;Cedula del ciudadano\n\nIngrese la cedula: \n&quot;;
          cin &gt;&gt; aux-&gt;cedula;
  
          aux-&gt;nxt = NULL;
  
          if (cab == NULL)
          {
              cab = aux;
          }
          else
          {
              aux1-&gt;nxt = aux;
          }
  
          aux1 = aux;
  
          cout &lt;&lt; &quot;Desea agregar mas personas? [si=1/no=2]&quot;; // TL: add more data?
          cin &gt;&gt; opc;
  
          system(&quot;cls&quot;);
      } while (opc == 1);
  }
  
  void eliminarXdato()
  {
     // lista cab, aux, aux1;
      int tkn = 0; // tried to use tkn for equal value check
  
      cout &lt;&lt; &quot;Ingrese la cedula a eliminar: \n&quot;;
      cin &gt;&gt; tkn;
  
      if (cab != NULL &amp;&amp; cab-&gt;cedula == tkn)
      {
          cab = cab-&gt;nxt;
          delete cab;
      }
  
      while (cab != NULL &amp;&amp; cab-&gt;cedula != tkn)
      {
          aux1 = cab;
          cab = cab-&gt;nxt;
      }
  
      if (cab == NULL)
          aux1-&gt;nxt = cab-&gt;nxt;
  
      delete cab;
  }
  </code></pre>