PedroHMM

DESAFIOS

Apr 8th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5.  
  6. typedef struct estrutura
  7. {
  8.         int chave;
  9.         estrutura *prox;
  10. } NO;
  11.  
  12. typedef struct
  13. {
  14.         NO* inicio;
  15. } LISTA;
  16.  
  17. void inicializar(LISTA *l)
  18. {
  19.      l->inicio = NULL;
  20. }
  21.  
  22. void exibir(LISTA *l)
  23. {
  24.      NO* p = l->inicio;
  25.      while(p)
  26.      {
  27.              printf("%i\t", p->chave);
  28.              p = p->prox;
  29.      }
  30.      printf("\n");
  31. }
  32.  
  33. NO* buscar(LISTA *l, int chave, NO* *ant)
  34. {
  35.     NO* p = l->inicio;
  36.     *ant = NULL;
  37.     while(p)
  38.     {
  39.             if(p->chave == chave) return p;
  40.             *ant = p;
  41.             p = p->prox;
  42.     }
  43.     return NULL;
  44. }
  45.  
  46. bool anexar(LISTA *l, int chave)
  47. {
  48.      NO* ant;
  49.      NO* novo = buscar(l, chave, &ant);
  50.      if(novo) return false;
  51.      novo = (NO*) malloc(sizeof(NO));
  52.      novo->chave = chave;
  53.      if(!l->inicio)
  54.      {
  55.                    l->inicio = novo;
  56.                    novo->prox = NULL;
  57.      }
  58.      else
  59.      {
  60.          if(!ant)
  61.          {
  62.                  novo->prox = l->inicio;
  63.                  l->inicio = novo;
  64.          }
  65.          else
  66.          {
  67.                  novo->prox = ant->prox;
  68.                  ant->prox = novo;  
  69.          }
  70.      }
  71.      return true;
  72. }
  73.  
  74. int size(LISTA *l)
  75. {
  76.     NO* p = l->inicio;
  77.     int cont = 0;
  78.     while(p)
  79.     {
  80.             cont++;
  81.             p = p->prox;
  82.     }
  83.     return cont;
  84. }
  85.  
  86. bool inserir(LISTA *l, int chave, int pos)
  87. {
  88.      if(pos <0) return false;
  89.      NO* ant;
  90.      NO* novo = buscar(l, chave, &ant);
  91.      if(novo) return false;
  92.       novo = (NO*) malloc(sizeof(NO));
  93.       novo->chave =chave;
  94.      if(pos == 0)
  95.      {
  96.             novo->prox = l->inicio;
  97.             l->inicio = novo;
  98.      }
  99.      if(pos >= size(l))
  100.      {
  101.             ant->prox = novo;
  102.             novo->prox = NULL;
  103.      }
  104.      else
  105.          {
  106.              int cont=0;
  107.              NO* p = l->inicio;
  108.              while(p)
  109.              {
  110.                      if(cont == pos -1)
  111.                      {
  112.                              novo->prox = p->prox;
  113.                              p->prox =novo;
  114.                              return true;
  115.                      }
  116.                      else
  117.                      {
  118.                          cont ++;
  119.                          p =p->prox;
  120.                      }
  121.              }        
  122.          }
  123.          return true;
  124. }
  125.  
  126. bool excluir(LISTA *l, int chave)
  127. {
  128.      NO* ant;
  129.      NO* p = buscar(l, chave, &ant);
  130.      if(!p) return false;
  131.      if(!ant) l->inicio = p->prox;
  132.      else ant->prox = p->prox;
  133.      free(p);
  134.      return true;
  135. }
  136.  
  137. void destruir(LISTA *l)
  138. {
  139.      NO* p =l->inicio;
  140.      NO* aux;
  141.      while(p)
  142.      {
  143.              aux = p->prox;
  144.              free(p);
  145.              p = aux;
  146.      }
  147.      l->inicio = NULL;
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. //Comparar 2 listas, se forem identicas return true : return false;
  155.  
  156. bool compararListas(LISTA *l1, LISTA *l2)
  157. {
  158.      if(size(l1) != size(l2)) return false;
  159.      
  160.      NO* p1 = l1->inicio;
  161.      NO* p2 = l2->inicio;
  162.      
  163.      while(p1)
  164.      {
  165.           if(p1->chave != p2->chave) return false;
  166.          
  167.           p1 = p1->prox;
  168.           p2 = p2->prox;
  169.      }
  170.      printf("Sao inguais\n");
  171.      return true;
  172. }
  173.  
  174. //Comparar 2 listas, se todos os elementos de uma existirem na outra, return true : return false;
  175.  
  176. bool compararElementos(LISTA *l1, LISTA *l2)
  177. {
  178.      if(size(l1) != size(l2)) return false;
  179.      
  180.      NO* p1 = l1->inicio;
  181.      NO* p2 = l2->inicio;
  182.      
  183.      int cont = 0;
  184.      
  185.      while(p1)
  186.      {
  187.          while(p2)
  188.          {
  189.               if(p1->chave != p2->chave)
  190.               {
  191.                            p2 = p2->prox;      
  192.               } else {
  193.                      cont++;
  194.                      p2 = l2->inicio;
  195.                      break;
  196.               }
  197.          }
  198.          
  199.          p1 = p1->prox;
  200.      }
  201.      
  202.      if(cont == size(l1)) {printf("Sao inguais\n");return true;}
  203.      
  204.      
  205.      return false;
  206. }    
  207.  
  208. //Concatenar 2 listas
  209.  
  210. NO* getUltimo(LISTA *l)
  211. {
  212.     NO* p = l->inicio;
  213.    
  214.     while(p->prox) {
  215.      p = p->prox;        
  216.     }
  217.    
  218.     return p;
  219. }
  220.  
  221. bool concatenar(LISTA *l1, LISTA *l2)
  222. {
  223.      
  224.     NO* p = l1->inicio;
  225.    
  226.     while(p->prox) {// pode subistiruir pelo get ultimo
  227.                    p = p->prox;        
  228.     }
  229.      
  230.      p->prox = l2->inicio;
  231.      
  232.      return true;
  233. }
  234.  
  235. //Inverter uma lista
  236.  
  237. bool inverterLista(LISTA *l)
  238. {
  239.     NO* atual = l->inicio;
  240.     NO* proximo = l->inicio;
  241.     NO* temp = l->inicio;
  242.    
  243.     while( proximo != NULL ){
  244.         // preserva ponteiro para caminhar no encadeamento
  245.         temp = temp->prox;
  246.    
  247.         // inverte o caminho do elemento posterior ao atual
  248.         proximo->prox = atual;
  249.    
  250.         // determina o proximo elemento a ser processado
  251.          atual = proximo;
  252.    
  253.         // determina o elemento posterior
  254.         proximo = temp;
  255.     }
  256.    
  257.    // remove link do primeiro elemento com o segundo elemento
  258.     l->inicio->prox = NULL;
  259.  
  260.     // aponta para a nova cabeca da lista
  261.     l->inicio = atual;
  262.    
  263.     return true;
  264. }
  265.  
  266. main()
  267. {
  268.       LISTA l;
  269.       LISTA l2;
  270.       inicializar(&l);
  271.       anexar(&l, 1);
  272.       anexar(&l, 2);
  273.       anexar(&l, 3);
  274.       exibir(&l);
  275.       getch();
  276.  
  277.       inicializar(&l2);
  278.       anexar(&l2, 1);
  279.       anexar(&l2, 4);
  280.       anexar(&l2, 3);
  281.       exibir(&l2);
  282.       getch();
  283.       inverterLista(&l);
  284.       exibir(&l);
  285.       getch();
  286. }
Advertisement
Add Comment
Please, Sign In to add comment