// UNION Y COMPLEMENTO DE CONJUNTOS CON LISTAS
#include <stdio.h>
#include <stdlib.h>
struct nodo
{
int num;
struct nodo *sig;
};
struct nodo *ConjA, *ConjB, *ConjResp, *Universo, *Comple_Conj;
struct nodo *CreaNodo(int num)
{
struct nodo *new = (struct nodo*)malloc(sizeof(struct nodo));
new->num = num;
new->sig = NULL;
return new;
}
void inicializa(void) { ConjA = ConjB = ConjResp = Universo = Comple_Conj = NULL; }
void PonerEnListaA(struct nodo *new)
{
if ( !ConjA ) ConjA = new;
else { new->sig = ConjA; ConjA = new; }
}
void PonerEnListaB(struct nodo *new)
{
if ( !ConjB ) ConjB = new;
else { new->sig = ConjB; ConjB = new; }
}
void CreaConjunto(int tam, int V[], char Conj)
{
int x = 0;
struct nodo *new=NULL;
for (x = 0; x<tam; x++)
{
new = CreaNodo(V[x]);
(Conj=='A') ? PonerEnListaA(new) : PonerEnListaB(new);
}
}
void Muestra(struct nodo *Conj)
{
struct nodo *aux = Conj;
while (aux) { printf("%d\t", aux->num); aux = aux->sig; }
printf("\n\n");
}
int Existe(int num)
{
struct nodo *aux = ConjResp;
while(aux)
{
if (num == aux->num) return 1;
aux = aux->sig;
}
return 0;
}
void Union(void)
{
struct nodo *aux=NULL, *new = NULL;
aux = ConjA;
while(aux) // COPIAMOS EL CONJUNTO A EN RESP
{
new = CreaNodo(aux->num);
if ( !ConjResp ) ConjResp = new;
else { new->sig = ConjResp; ConjResp = new; }
aux = aux->sig;
}
aux = ConjB;
while(aux) // COPIAMOS DE CONJB LOS QUE NO ESTAN REPETIDOS
{
if ( !Existe(aux->num) )
{
new = CreaNodo(aux->num);
if ( !ConjResp ) ConjResp = new;
else { new->sig = ConjResp; ConjResp = new; }
}
aux = aux->sig;
}
}
int RevisaComplemento(int num, struct nodo *aux)
{
while(aux)
{
if (num == aux->num) return 1;
aux = aux->sig;
}
return 0;
}
void Complemento(struct nodo *Conjunto)
{
struct nodo *U_aux = Universo, *new=NULL;
while ( U_aux )
{
if ( !RevisaComplemento(U_aux->num, Conjunto) )
{
new = CreaNodo(U_aux->num);
if ( !ConjResp ) ConjResp = new;
else { new->sig = ConjResp; ConjResp = new; }
}
U_aux = U_aux->sig;
}
}
void inicio()
{
int LA[] = {1, 2, 3, 4, 6};
int LB[] = {3, 5, 6, 7, 9};
CreaConjunto(5, LA, 'A');
CreaConjunto(5, LB, 'B');
printf("El conjunto A es: "); Muestra(ConjA);
printf("El conjunto B es: "); Muestra(ConjB);
Union(); Universo = ConjResp; printf("La unión es: "); Muestra(Universo);
ConjResp = NULL;
Complemento(ConjA); Comple_Conj = ConjResp;
printf("El complemento de A' es: "); Muestra(Comple_Conj);
}
int main(char **a, int b) { inicializa(); inicio(); }