Hello again guys,

this is the code:

struct nodo {

  char *nom_usuario;   //*user_name
  struct sockaddr_in IP;  
  struct nodo *siguiente; //*next

};

  struct lista {   //list
    struct nodo *primero;   //*first
    struct nodo *ultimo;    //*last

  };

...
...
...

void ordenar(struct lista* maestra); 
void ordenar(struct lista* maestra){
	
	struct nodo* aux;
	struct nodo* aux2;
	struct nodo* aux3;

	for(aux2=maestra->primero;aux2->siguiente!=NULL;aux2=aux2->siguiente){
		for(aux=aux2->siguiente; aux!=NULL;aux=aux->siguiente){
		
		if(strcmp(inet_ntoa(aux->IP.sin_addr),inet_ntoa(aux2->IP.sin_addr))==0)	{
		aux3=aux2->siguiente;
		aux2->siguiente=aux;
		aux->siguiente=aux3;
		printf("I'm just checking it's entered the if clause" \n);}

		
		}
	}

	for(aux=maestra->primero;aux!=NULL;aux=aux->siguiente){
	printf("%s \n",inet_ntoa(aux->IP.sin_addr));

	}

	printf("---------------------- \n");


	}

My situation is as follows:

I have a master list which contains a series of "nodes" (called nodo in the code). The list is empty at first, so one of the things i had to do is to implement the insert operation, which I did, and it works like a charm so far.

The other thing i had to do, is to implement an option which allows us to arrange the nodes of the list according to their IP, having to link together those who have the same IP, for example:

If i have 4 nodes with these IPs

1) 192.168.2.1 => maestra-> primero (first)
2) 172.16.0.1
3) 84.121.121.121
4) 192.168.2.1 => maestra->ultimo (last)

the result must be like this:

1) 192.168.2.1 => maestra-> primero (first)
2) 192.168.2.1
3) 172.16.0.1
4) 84.121.121.121 => maestra->ultimo (last)

It wasn't stated whether we must put the "smaller" Ips first ( 84.121, 172.16,192....) so that's not a problem.

My problem is that i've tried to implement a solution to that (stated in the code), but it only makes the program enter an endless loop (seems like endless loops love me). So i would be very grateful if any of you guys could give a hint on how to fix this and make it work.

My other problem would be to know where and how to state the new "ultimo".

That for loop in line 40 just shows the list of IPs, i made it just to see whether the process works or not.


Thanks a lot in advance.

The reason why you are getting endless loops is because when you re arrange the pointers, the list becomes circular( Work out your code on a piece of paper if you dont believe me) . Another solution could be to delete the node(totally disconnect it from the list) and then call an insert routine to insert the node at the correct place . I believe this approach will require slightly more code but will be simpler to implement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.