Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #include "conjunto.h"
- int max(int a, int b){
- if(a>b)
- return a;
- return b;
- }
- int min(int a, int b){
- if(a<b)
- return a;
- return b;
- }
- void unionAux(TConjunto* a,TConjunto* b,TConjunto* c){
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n != NULL){
- insertar(c,n->value);
- n = n->next;
- }
- n = b->first;
- while(n != NULL){
- insertar(c,n->value);
- n = n->next;
- }
- }
- void interseccion(TConjunto* a,TConjunto* b,TConjunto* c){
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n != NULL){
- if(miembro(b,n->value))
- insertar(c,n->value);
- n = n->next;
- }
- }
- void diferencia(TConjunto* a,TConjunto* b,TConjunto* c){
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n != NULL){
- if(!miembro(b,n->value))
- insertar(c,n->value);
- n = n->next;
- }
- }
- int miembro(TConjunto *a, TInfo x){
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n!=NULL){
- if(n->value == x)
- return 1;
- n = n->next;
- }
- return 0;
- }
- void initConjunto(TConjunto *a){
- a->first = NULL;
- a->last = NULL;
- a->nElem = 0;
- }
- void insertar(TConjunto *a, TInfo x){
- TNode *n = malloc(sizeof(TNode));
- TNode *p = malloc(sizeof(TNode));
- n = a->first;
- if(a->first == NULL){
- p->value = x;
- p->next = NULL;
- a->first = p;
- a->last = p;
- }else{
- while(n != NULL){
- if(n->value == x)
- return;
- n = n->next;
- }
- p -> value = x;
- p->next = NULL;
- a->last->next = p;
- a->last = p;
- }
- a->nElem++;
- }
- void remover(TConjunto *a, TInfo x){
- TNode *n = malloc(sizeof(TNode));
- TNode *nant = malloc(sizeof(TNode));
- n = a->first;
- if(a->first == NULL){
- return;
- }else{
- if(a->first->value == x){
- a->first = a->first->next;
- free(n);
- a->nElem--;
- }else{
- while(n != NULL){
- if(n->value == x){
- if(n->next == NULL)
- a->last = nant;
- nant->next = n->next;
- free(n);
- a->nElem--;
- break;
- }
- nant = n;
- n = n->next;
- }
- }
- }
- }
- int minimo(TConjunto *a){
- int ans = INT_MAX;
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n != NULL){
- ans = min(ans,n->value);
- n = n->next;
- }
- return ans;
- }
- int maximo(TConjunto *a){
- int ans = INT_MIN;
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n != NULL){
- ans = max(ans,n->value);
- n = n->next;
- }
- return ans;
- }
- int igual(TConjunto* a,TConjunto* b){
- if(a->nElem != b->nElem)
- return 0;
- TNode *n = malloc(sizeof(TNode));
- n = b->first;
- int i;
- for(i=0; i<(a->nElem); i++){
- if(!miembro(a,n->value))
- return 0;
- n = n->next;
- }
- return 1;
- }
- void imprimir(TConjunto *a){
- TNode *n = malloc(sizeof(TNode));
- n = a->first;
- while(n != NULL){
- printf("%d ",n->value);
- n = n->next;
- }
- printf("\n");
- }
Add Comment
Please, Sign In to add comment