Advertisement
davide1409

ese1.c

Nov 23rd, 2021
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "list.h"
  3. #include <stdlib.h>
  4.  
  5. int is_in(list_t* M, int val){
  6.     if(M == NULL)
  7.         exit(-1);
  8.     if(empty(M))
  9.         return 0;
  10.  
  11.  
  12.     node_t *p = head(M);
  13.     while(p!=NULL){
  14.         if(get_val(p) == val)
  15.             return 1;
  16.         p = next(p);
  17.     }
  18.  
  19.     return 0;
  20. }
  21.  
  22. list_t* noTwo(list_t *L){
  23.     if(L == NULL)
  24.         return NULL;
  25.     if(head(L) == NULL)
  26.         return NULL;
  27.  
  28.     list_t *M = malloc(sizeof(*M));
  29.     if(M == NULL)
  30.         return NULL;
  31.  
  32.     init_list(M);
  33.  
  34.     int counter=0;
  35.     node_t *h = head(L);
  36.     node_t *p = h;
  37.     node_t *q = h;
  38.     int p_val;
  39.     node_t *nodo = NULL;
  40.  
  41.     while(p != NULL){
  42.         p_val = get_val(p);
  43.         while(q != NULL){
  44.             if(get_val(q) == p_val)
  45.                 counter++;
  46.  
  47.             q = next(q);
  48.         }
  49.  
  50.         if(counter!=2){
  51.             if(!is_in(M, p_val)){
  52.                 nodo = insert(M, nodo, p_val);
  53.                 if(nodo == NULL)
  54.                     exit(-1);
  55.             }
  56.         }
  57.  
  58.         q = h;
  59.         counter = 0;
  60.         p = next(p);
  61.     }
  62.  
  63.     return M;
  64. }
  65.  
  66. int main(int argc, char **argv){
  67.     if(argc == 1){
  68.         printf("Usage %s: %s <num1, num2, num3, ...>\n", argv[0], argv[0]);
  69.         exit(-1);
  70.     }
  71.    
  72.     int vals[argc-1];
  73.     for(int i = 0; i!=argc-1; i++)
  74.         vals[i] = atoi(argv[i+1]);
  75.    
  76.     // vals ha tutti i valori che ci servono ora!
  77.  
  78.     list_t L;
  79.     if(init_list(&L))
  80.         return -1;
  81.  
  82.     node_t *nodo = NULL;
  83.     for(int i = 0; i!=argc-1; i++){
  84.         nodo = insert(&L, nodo, vals[i]);
  85.         if(nodo == NULL)
  86.             return -1;
  87.     }          
  88.     printf("La lista dei valori dati: ");
  89.     print_list(&L);
  90.  
  91.  
  92.     list_t *M = noTwo(&L);
  93.     if(M == NULL)
  94.         return -1;
  95.    
  96.     printf("\nLa nuova lista senza i valori che compaiono esattamente due volte: ");
  97.     print_list(M);
  98.    
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement