I've re-written most of your main as I couldn't work out exactly what you were doing, but I think this is the kind of thing your after.
I would recommend wrapping this in some kind of class, or at least breaking main up into separate functions as all the nested loops make it pretty hard to debug.
I've replaced your file input for console input (ctrl-z to end), and I've been unable to delete the memory allocated for each name using MSVC in debug mode (not sure what I've overlooked) so if you want to debug the program as it stands you'll have to comment out the lines
delete [] nuevo->nombre; // in block for duplicate name check
and
delete [] aux->nombre; // in final cleanup block
Code:
#include <iostream>
#include <cstring>
using namespace std;
struct nodo1{
char *nombre;
int contador;
struct nodo1 *next;
struct nodo2 *sig;
};
struct nodo2
{
int x;
struct nodo2 *sig;
};
typedef struct nodo1 reg1;
typedef struct nodo2 reg2;
reg2* nuevo_linea(int linea)
{
reg2 *nuevo2;
nuevo2=new reg2;
nuevo2->x=linea;
nuevo2->sig=NULL;
return nuevo2;
}
reg1* nuevo_nodo1(char *a,int tam,int linea)
{
reg1 *nuevo;
nuevo=new reg1;
nuevo->nombre=new char[tam];
strcpy(nuevo->nombre,a);
nuevo->contador=1;
nuevo->next=NULL;
reg2 *nuevo_lista=nuevo_linea(linea);
nuevo->sig=nuevo_lista;
return nuevo;
}
void mayor(reg1*&aux,reg1*&nuevo)
{
while(aux!=NULL)
{
if(aux->next==NULL)
{
aux->next=nuevo;
break;
}
else if(strcmp(nuevo->nombre,aux->next->nombre)<0)
{
nuevo->next=aux->next;
aux->next=nuevo;
aux=aux->next;
break;
}
aux=aux->next;
}
}
int main()
{
int linea =1;
reg1* lista=NULL;
reg1* nuevo;
reg1* aux;
char nombre[20]={0};
int tam;
while (1)
{
cin.get(nombre,19,'\n');
cin.ignore(80,'\n');
if (cin.eof())break;
tam=strlen(nombre);
nuevo = nuevo_nodo1(nombre,tam,linea);
if(lista==NULL)
{
lista = nuevo;
linea++;
continue;
}
aux= lista;
while(aux!=NULL)
{
if (strcmp(nuevo->nombre,aux->nombre)==0)
{
reg2* nuevo2 = new reg2;
nuevo2->x=nuevo->sig->x;
nuevo2->sig=NULL;
while(aux->sig->sig!=NULL)
aux->sig = aux->sig->sig;
aux->sig->sig=nuevo2;
aux->contador++;
linea++;
delete [] nuevo->nombre;
delete nuevo->sig;
delete nuevo;
nuevo = NULL;
break;
}
aux=aux->next;
}
if(nuevo ==NULL)
continue;
if (strcmp(nuevo->nombre,lista->nombre)<0)
{
nuevo->next=lista;
lista=nuevo;
linea++;
continue;
}
aux=lista;
mayor(aux,nuevo);
linea++;
}
aux=lista;
while(aux!=NULL)
{
cout << aux->nombre << " Contador - " << aux->contador << " Linea - ";
while(aux->sig!=NULL)
{
cout << aux->sig->x << " - ";
aux->sig=aux->sig->sig;
}
cout << endl;
aux=aux->next;
}
aux=lista;
while(aux!=NULL)
{
reg1* aux2 = aux->next;
delete [] aux->nombre;
while(aux->sig!=NULL)
{
reg2* aux3 = aux->sig->sig;
delete aux->sig;
aux->sig=aux3;
}
delete aux;
aux=aux2;
}
return 0;
}