Advertisement
Guest User

Listas Duplamente

a guest
Oct 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.86 KB | None | 0 0
  1. // EXERCICIO A ) INSERIR NO FIM DA LISTA
  2.  
  3. void insereFim(int x, listadup *inicio, listadup *fim){
  4.     listadup novo;
  5.    
  6.     if(inicio == NULL) return;//lista existe?
  7.     if((*inicio) == NULL){//lista vazia?
  8.         return;
  9.     }else{
  10.         novo = getNode();
  11.        
  12.         if(novo == NULL){//não alocou memoria      
  13.             printf("\nnoh nao pode ser alocado\n");
  14.             system("pause");
  15.             return;
  16.         }else{//recebeu um endereço valido de memoria
  17.             novo -> info = x;
  18.             novo -> prox = NULL;
  19.             (*fim) -> prox = novo;
  20.             novo -> ant = *fim;
  21.             *fim = novo;
  22.         }
  23.     }
  24.     return;
  25. }
  26.  
  27. // EXERCICIO B ) CONCATENAR 2 LISTAS
  28.  
  29. void concatenaLista(listadup *inicio, listadup *fim, listadup *inicio1, listadup *fim1){
  30.    
  31.     //fazer as verificações
  32.    
  33.     listadup aux;
  34.     aux = *inicio;
  35.    
  36.     while(aux != NULL){
  37.         aux = aux -> prox;
  38.     }
  39.    
  40.     aux -> prox = *inicio1;
  41.     free(inicio1);
  42.     *fim = *fim1;
  43.     free(fim1);
  44.    
  45.     return;
  46. }
  47.  
  48.  
  49. // EXERCICIO C ) LIBERAR TODOS OS NOHS DE UMA LISTA
  50.  
  51. void liberaLista(listadup *inicio){
  52.     if(inicio == NULL){
  53.         printf("\nLista Vazia\n");
  54.         system("pause");
  55.     }else{
  56.        
  57.         listadup aux;
  58.        
  59.         while((*inicio) != NULL){
  60.             aux = *inicio;
  61.             *inicio = (*inicio) -> prox;
  62.             free(aux);
  63.         }
  64.         *fim = *inicio;
  65.     }
  66.     return;
  67. }
  68.  
  69. // EXERCICIO D) INVERTER LISTA
  70. void inverteLista(listadup *inicio, listadup *fim, listadup *inicio1, listadup *fim1){
  71.    
  72.     if(inicio == NULL) return; //lista nao existe nao existe endereço de inicio
  73.     if((*inicio) == NULL) return; // não existe nada sendo apontado para o inicio 
  74.    
  75.     while((*inicio) != NULL){
  76.         insereFim((*inicio) -> info,inicio1,fim1);
  77.         *inicio = (*inicio) -> prox;
  78.     }
  79.    
  80.     *fim = *inicio;
  81.    
  82.     return;
  83. }
  84.  
  85. // EXERCICIO E ) REMOVER ULTIMO ELEMENTO DA LISTA
  86.  
  87. void removerUltimo(listadup *inicio)
  88. {
  89.     listadup aux;
  90.  
  91.     if(inicio == NULL) return; //lista existe?
  92.     if((*inicio) == NULL) return;//lista vazia?
  93.    
  94.     aux = *inicio;
  95.    
  96.     while(aux -> prox == NULL){//enquanto existir mais elementos na lista
  97.         aux = aux -> prox;
  98.     }
  99.    
  100.     if(aux -> ant == NULL){//é o primeiro noh?
  101.         *inicio = aux -> prox;//vou apontar o inicio para o prox, que na vdd é o NULL
  102.     }else{
  103.         aux -> ant -> prox = NULL;//se nao for o primeiro noh,
  104.     }
  105.    
  106.     free(aux);
  107.     return;
  108. }
  109.  
  110. // EXERCICIO F ) REMOVER ENESIMO ELEMENTO DA LISTA
  111.  
  112. void removerQualquer(int x, listadup *inicio){
  113.     listadup aux;
  114.  
  115.     if(inicio == NULL) return; //lista existe?
  116.     if((*inicio) == NULL) return;//lista vazia?
  117.    
  118.     aux = *inicio;
  119.    
  120.     while(aux != NULL && aux -> info != x){//enquanto nao encontrar o elemento
  121.         aux = aux -> prox;//caminho na lista até bater na regra
  122.     }
  123.    
  124.     if(aux == NULL){//leu toda lista e chegou em null
  125.         printf("Elemento nao encontrado\n");
  126.         return;
  127.     }
  128.     if(aux -> ant == NULL{//eh o primeiro elemento?
  129.         *inicio = aux -> prox;
  130.     }else{//tem mais de um elemento?
  131.         aux -> ant -> prox = aux -> prox;
  132.     }
  133.    
  134.     if(aux -> prox != NULL){
  135.         aux -> prox -> ant = aux -> ant;
  136.     }
  137.    
  138.     free(aux);
  139.     return;
  140. }
  141.  
  142. // EXERCICIO G ) COMBINAR LISTAS
  143.  
  144. void combinarListas(listadup inicio, listadup inicio1, listadup inicio2, listadup *inicio2, listadup *fim2){
  145.     //considerando ja que as 2 listas estao ordenadas
  146.    
  147.     inicio2 = inicio;
  148.    
  149.     listadup aux = inicio;
  150.    
  151.     while(aux != NULL){
  152.         aux = aux -> prox;
  153.     }
  154.    
  155.     aux -> ant -> prox = inicio1;
  156.     fim = fim1;
  157.     fim2 = fim;
  158.  
  159.     return;
  160. }
  161.  
  162. // EXERCICIO H ) UNIAO DOS ELEMENTOS DE DUAS LISTAS
  163.  
  164. void uniaoListas(listadup inicio, listadup inicio2, listadup *inicio3, listadup *fim3){
  165.    
  166.     listadup aux = *inicio;
  167.     listadup aux2 = *inicio2;
  168.  
  169.     *inicio3 = concatenaLista()
  170.    
  171.     exibeLista(inicio);
  172.     exibeLista(inicio2);
  173.     exibeLista(inicio3);   
  174. }
  175.  
  176. // EXERCICIO I ) INTERSECCAO DOS ELEMENTOS DE DUAS LISTAS
  177.  
  178. void interseccaoListas(listadup inicio, listadup inicio2, listadup *inicio3, listadup *fim3){
  179.  
  180.     listadup aux = *inicio;
  181.     listadup aux2 = *inicio2;
  182.  
  183.     while(aux != NULL){
  184.         while(aux2 != NULL){
  185.             if(aux -> info != aux2 -> info){
  186.                 if((*inicio3) == NULL){
  187.                     insereInicio(aux->info,inicio3,fim3);
  188.                 }else{
  189.                     insereFim(aux->info,inicio3,fim3);
  190.                 }
  191.             }else{
  192.                 aux2 = aux2 -> prox;
  193.             }
  194.         }
  195.         aux = aux -> prox;
  196.     }
  197.    
  198.     printf("\n");
  199.     exibeLista(inicio);
  200.     printf("\n");
  201.     exibeLista(inicio2);
  202.     printf("\n");
  203.     exibeLista(inicio3);   
  204.     printf("\n");
  205.    
  206.     return;
  207. }
  208.  
  209. // EXERCICIO J ) INSERIR ELEMENTO APOS ENESIMO TERMO
  210.  
  211. void insereApos(int x, int y, listadup *inicio, listadup *fim)
  212. {
  213.     listadup novo,aux,aux2;
  214.    
  215.     if((*inicio) == NULL){
  216.         printf("Lista Vazia\n");
  217.         system("pause");
  218.     }else{
  219.         if((*fim) -> info == y ){
  220.             insereFim(x,inicio,fim);
  221.         }else{
  222.             aux = *inicio;
  223.             while(aux != NULL && aux -> info != y){
  224.                 aux = aux -> prox;
  225.             }
  226.             if(aux == NULL){
  227.                 printf("Elemento nao encontrado\n");
  228.                 system("pause");
  229.             }else{
  230.                 novo = getNode();
  231.                 if(novo == NULL){
  232.                     printf("noh nao alocado\n");
  233.                     system("pause");
  234.                 }else{
  235.                     novo -> info = x;
  236.                     novo -> ant = aux;
  237.                     novo -> prox = aux -> prox;
  238.                     aux -> prox = novo;
  239.                     aux -> prox -> ant = novo
  240.                 }
  241.             }
  242.         }
  243.     }
  244.     return;
  245. }
  246.  
  247. // EXERCICIO K ) REMOVER CADA SEGUNDO ELEMENTO
  248.  
  249. void segundoE(listadup *inicio, listadup *fim){
  250.    
  251.     if(inicio == NULL) return; //lista existe?
  252.     if((*inicio) == NULL) return;//lista vazia?
  253.    
  254.     listadup aux,aux2;
  255.     aux = *inicio;
  256.    
  257.     int x = 0;
  258.    
  259.     while(aux -> prox != NULL){
  260.         if(x % 2 == 0){
  261.             aux2 = aux -> prox;
  262.             aux -> prox = aux2 -> prox;
  263.             aux -> prox -> ant = aux2 -> ant;
  264.             free(aux2);
  265.         }else{
  266.             aux = aux -> prox;
  267.         }
  268.         x++;
  269.     }
  270.     *fim = aux;
  271.     return;
  272. }
  273.  
  274. // EXERCICIO L ) ORDENAR ASCENDENTE
  275.  
  276. void ordena(listadup *inicio){
  277.    
  278.     if(inicio == NULL) return; //lista existe?
  279.     if((*inicio) == NULL) return;//lista vazia?
  280.    
  281.     int x;
  282.    
  283.     listadup aux;
  284.    
  285.     aux = (*inicio) -> prox;
  286.  
  287.     for (inicio; inicio -> prox != NULL; *inicio = (*inicio) -> prox){
  288.         for (aux; aux != NULL; aux = aux -> prox){
  289.             if ((*inicio) -> info > aux -> info){
  290.                 x = (*inicio) -> info;
  291.                 (*inicio) -> info = aux -> info;
  292.                 aux -> info = x;
  293.             }
  294.         }
  295.     }
  296.     return;
  297. }
  298.  
  299. // EXERCICIO M ) RETORNAR A SOMA DOS ELEMENTOS
  300.  
  301. void somaL(listadup inicio){
  302.     if(inicio == NULL) return;//lista existe?
  303.     if((*inicio) == NULL) return;//lista vazia?
  304.    
  305.     int s = 0;
  306.    
  307.     listadup aux = *inicio;
  308.  
  309.     while(aux != NULL){
  310.         s += aux->info;
  311.         aux = aux-> prox;
  312.     }
  313.    
  314.     system("pause");
  315.     system("cls");
  316.     return s;
  317. }
  318.  
  319. // EXERCICIO N ) EXIBE OS ELEMENTOS DE UMA LISTA
  320.  
  321. void exibeLista(listadup inicio, listadup fim){
  322.    
  323.     if(inicio == NULL) return; //lista existe?
  324.     if((*inicio) == NULL) return;//lista vazia?
  325.    
  326.     int opt;
  327.    
  328.     aux = inicio;
  329.     aux2 = fim;
  330.    
  331.     printf("\n[1] - Deseja exibir apartir do Inicio\n[2] - Deseja exibir apartir do fim?:");
  332.     scanf("%d",&opt);
  333.    
  334.     switch(opt){
  335.         case 1:
  336.             while(aux != NULL){
  337.                 printf("[%d] ",aux -> info);
  338.                 aux = aux -> prox;
  339.             }
  340.             system("pause");
  341.             break;
  342.         case 2:
  343.             while(aux2 != NULL){
  344.                 printf("[%d] ",aux -> info);
  345.                 aux2 = aux2 -> ant;
  346.             }
  347.             system("pause");
  348.             break;
  349.         default:
  350.             printf("\nPor favor, escolha uma opcao valida.\n");
  351.             break;
  352.     }
  353.     return;
  354. }
  355.  
  356. // EXERCICIO N ) RETORNAR O NUMERO DE ELEMENTOS DE UMA LISTA
  357.  
  358. int exibeTam(listadup inicio){
  359.    
  360.     if(inicio == NULL) return; //lista existe?
  361.     if((*inicio) == NULL) return;//lista vazia?
  362.    
  363.     listadup aux = *inicio;
  364.    
  365.     while ((*inicio) != NULL){
  366.         i++;
  367.         *inicio = (*inicio) -> prox;
  368.     }
  369.    
  370.     return i;
  371. }
  372.  
  373. // EXERCICIO O ) MOVE UM NOH (N) TERMOS A FRENTE
  374.  
  375. void moveLista(listadup *inicio){
  376.     int x,y,opt;
  377.     listadup aux,novo;
  378.    
  379.     if(inicio == NULL) return;//lista existe? Existe endereço de memoria da lista?
  380.     if((*inicio) == NULL) return;//lista vazia? Inicio aponta para algum noh?
  381.    
  382.     exibeLista(inicio);
  383.    
  384.     printf("\nQual elemento deseja Mover: ");
  385.     scanf("%d",&x);
  386.    
  387.     //usuario informa qual elemento ele quer mover
  388.     //removo o noh, e insiro de acordo com que o usuario quiser
  389.     removerQualquer(x,inicio);
  390.    
  391.     printf("\nDeseja inserir\n[1] - Antes\n[2] - Apos?: ");
  392.     scanf("%d",&opt);
  393.    
  394.     switch(opt){
  395.         case 1:
  396.             printf("\nDeseja inserir antes de que elemento?: ");
  397.             scanf("%d",&y);
  398.             insereAntes(x,y,inicio,fim);
  399.             break;
  400.         case 2:
  401.             printf("\nDeseja inserir Apos que elemento?: ");
  402.             scanf("%d",&y);
  403.             insereApos(x,y,inicio,fim);
  404.             break;
  405.         default:
  406.             printf("\n[+] Opcao Invalida, por favor, escolha uma opcao\n");
  407.             break;
  408.     }
  409.    
  410.     exibeLista(inicio);
  411.     return;
  412. }
  413.  
  414. // EXERCICIO P) CRIAR COPIA DE UMA LISTA
  415.  
  416. void criaCP(listadup inicio, listadup *inicioCP, listadup *fimCP)
  417. {
  418.     listadup aux;
  419.    
  420.     if(inicio == NULL) return;//lista existe?
  421.     if((*inicio) == NULL) return;//lista vazia?
  422.    
  423.     aux = *inicio;
  424.    
  425.     while(aux != NULL){
  426.         if(inicioCP == NULL){
  427.             insereInicio(aux->info,inicioCP);
  428.         }else{
  429.             insereFim(aux->info,inicioCP,fimCP);
  430.         }
  431.         aux = aux -> prox;
  432.     }
  433.     return;
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement