Advertisement
Guest User

Estrutura de Dados 2 - Listas Duplamente

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