eduardovp97

conjunto.c

Oct 24th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include "conjunto.h"
  5.  
  6. int max(int a, int b){
  7.     if(a>b)
  8.         return a;
  9.     return b;
  10. }
  11.  
  12. int min(int a, int b){
  13.     if(a<b)
  14.         return a;
  15.     return b;
  16. }
  17.  
  18. void unionAux(TConjunto* a,TConjunto* b,TConjunto* c){
  19.     TNode *n = malloc(sizeof(TNode));
  20.     n = a->first;
  21.     while(n != NULL){
  22.         insertar(c,n->value);
  23.         n = n->next;
  24.     }
  25.     n = b->first;
  26.     while(n != NULL){
  27.         insertar(c,n->value);
  28.         n = n->next;
  29.     }
  30. }
  31.  
  32. void interseccion(TConjunto* a,TConjunto* b,TConjunto* c){
  33.     TNode *n = malloc(sizeof(TNode));
  34.     n = a->first;
  35.     while(n != NULL){
  36.         if(miembro(b,n->value))
  37.             insertar(c,n->value);
  38.         n = n->next;
  39.     }
  40. }
  41.  
  42. void diferencia(TConjunto* a,TConjunto* b,TConjunto* c){
  43.     TNode *n = malloc(sizeof(TNode));
  44.     n = a->first;
  45.     while(n != NULL){
  46.         if(!miembro(b,n->value))
  47.             insertar(c,n->value);
  48.         n = n->next;
  49.     }
  50. }
  51.  
  52. int miembro(TConjunto *a, TInfo x){
  53.     TNode *n = malloc(sizeof(TNode));
  54.     n = a->first;
  55.     while(n!=NULL){
  56.         if(n->value == x)
  57.             return 1;
  58.         n = n->next;
  59.     }
  60.     return 0;
  61. }
  62.  
  63. void initConjunto(TConjunto *a){
  64.     a->first = NULL;
  65.     a->last = NULL;
  66.     a->nElem = 0;
  67. }
  68.  
  69. void insertar(TConjunto *a, TInfo x){
  70.     TNode *n = malloc(sizeof(TNode));
  71.     TNode *p = malloc(sizeof(TNode));
  72.     n = a->first;
  73.     if(a->first == NULL){
  74.         p->value = x;
  75.         p->next = NULL;
  76.         a->first = p;
  77.         a->last = p;
  78.     }else{
  79.         while(n != NULL){
  80.             if(n->value == x)
  81.                 return;
  82.             n = n->next;
  83.         }
  84.         p -> value = x;
  85.         p->next = NULL;
  86.         a->last->next = p;
  87.         a->last = p;
  88.     }
  89.     a->nElem++;
  90. }
  91.  
  92. void remover(TConjunto *a, TInfo x){
  93.     TNode *n = malloc(sizeof(TNode));
  94.     TNode *nant = malloc(sizeof(TNode));
  95.     n = a->first;
  96.     if(a->first == NULL){
  97.         return;
  98.     }else{
  99.         if(a->first->value == x){
  100.             a->first = a->first->next;
  101.             free(n);
  102.             a->nElem--;
  103.         }else{
  104.             while(n != NULL){
  105.                 if(n->value == x){
  106.                     if(n->next == NULL)
  107.                         a->last = nant;
  108.                     nant->next = n->next;
  109.                     free(n);
  110.                     a->nElem--;
  111.                     break;
  112.                 }
  113.                 nant = n;
  114.                 n = n->next;
  115.             }
  116.         }
  117.     }
  118. }
  119.  
  120. int minimo(TConjunto *a){
  121.     int ans = INT_MAX;
  122.     TNode *n = malloc(sizeof(TNode));
  123.     n = a->first;
  124.     while(n != NULL){
  125.         ans = min(ans,n->value);
  126.         n = n->next;
  127.     }
  128.     return ans;
  129. }
  130.  
  131. int maximo(TConjunto *a){
  132.     int ans = INT_MIN;
  133.     TNode *n = malloc(sizeof(TNode));
  134.     n = a->first;
  135.     while(n != NULL){
  136.         ans = max(ans,n->value);
  137.         n = n->next;
  138.     }
  139.     return ans;
  140. }
  141.  
  142. int igual(TConjunto* a,TConjunto* b){
  143.     if(a->nElem != b->nElem)
  144.         return 0;
  145.     TNode *n = malloc(sizeof(TNode));
  146.     n = b->first;
  147.     int i;
  148.     for(i=0; i<(a->nElem); i++){
  149.         if(!miembro(a,n->value))
  150.             return 0;
  151.         n = n->next;
  152.     }
  153.     return 1;
  154. }
  155.  
  156. void imprimir(TConjunto *a){
  157.     TNode *n = malloc(sizeof(TNode));
  158.     n = a->first;
  159.     while(n != NULL){
  160.         printf("%d ",n->value);
  161.         n = n->next;
  162.     }
  163.     printf("\n");
  164. }
Add Comment
Please, Sign In to add comment