Advertisement
Guest User

Copiar Lista

a guest
Jul 4th, 2014
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct LISTA
  5. {
  6.     int info;
  7.     struct LISTA *prox;
  8. };
  9.  
  10. void limpaLista( struct LISTA **lista)
  11. {
  12.     if( (*lista)->prox != NULL )
  13.         limpaLista( &(*lista)->prox );
  14.  
  15.     free(*lista);
  16.  
  17.     *lista = NULL;
  18. }
  19.  
  20. void addItem (struct LISTA **lista)
  21. {
  22.     struct LISTA *atual = NULL;
  23.     atual = *lista;
  24.     system("cls");
  25.     printf("Digite a informacao\n");
  26.  
  27.     if ( *lista == NULL )
  28.     {
  29.         *lista = malloc( sizeof(struct LISTA) );
  30.         atual = *lista;
  31.     }
  32.     else
  33.     {
  34.         while( atual->prox != NULL )
  35.             atual = atual->prox ;
  36.  
  37.         atual->prox = malloc( sizeof(struct LISTA) );
  38.         atual = atual->prox;
  39.     }
  40.  
  41.     system("cls");
  42.     printf("Digite a informacao\n");
  43.     scanf("%d",&atual->info);
  44.  
  45.     atual->prox = NULL;
  46. }
  47.  
  48. void imprime(struct LISTA *lista)
  49. {
  50.     system("cls");
  51.  
  52.     struct LISTA *p;
  53.  
  54.     printf("Elementos: \n");
  55.     for (p = lista; p != NULL; p = p->prox)
  56.     {
  57.         printf( " * %d\n",p->info);
  58.     }
  59. }
  60.  
  61. struct LISTA * inicializa(void)
  62. {
  63.     return NULL;
  64. }
  65.  
  66. void copiar (struct LISTA **lista1, struct LISTA **lista2)
  67. {
  68.     struct LISTA *p = *lista1;
  69.     struct LISTA *q = *lista2;
  70.  
  71.     limpaLista( lista2 ); // Esvazia lista 2
  72.  
  73.     for (; p != NULL; p = p->prox ) // Percorre lista 1
  74.     {
  75.         if ( *lista2 == NULL ) // Se a lista estiver vazia, recebe o primeiro elemento
  76.         {
  77.             *lista2 = malloc( sizeof(struct LISTA) );
  78.             (*lista2)->info = p->info;
  79.             (*lista2)->prox = NULL;
  80.         }
  81.         else
  82.         {
  83.                 q->prox = malloc( sizeof(struct LISTA) ); // Cria elemento na posição vazia
  84.  
  85.                 q = q->prox; // Desloca para a posição do novo elemento
  86.  
  87.                 q->info = p->info; // Recebe valor da lista 1
  88.                 q->prox = NULL;
  89.         }
  90.     }
  91.  
  92.     printf("Copia completa\n");
  93. }
  94.  
  95. int main(void)
  96. {
  97.     int j = 0;
  98.     struct LISTA *lista1 = NULL;
  99.     struct LISTA *lista2 = NULL;
  100.  
  101.  
  102.     do
  103.     {
  104.         printf("Escolha uma das funcoes\n");
  105.         printf("Digite [1] para Inserir items\nDigite [2] para imprimir a LISTA\nDigite [3] para copiar a LISTA\nDigite [0] para encerrar o programa\n");
  106.         scanf("%d",&j);
  107.  
  108.         switch (j)
  109.         {
  110.             case 1:
  111.                 printf("Escolha se voce quer inserir na LISTA 1 ou na LISTA 2\n");
  112.                 scanf("%d",&j);
  113.  
  114.                 if ( j == 1 )
  115.                     addItem(&lista1);
  116.                 else
  117.                     if ( j == 2 )
  118.                         addItem(&lista2);
  119.                 break;
  120.  
  121.             case 2:
  122.                 printf("Escolha se voce quer imprimir a LISTA 1 ou a LISTA 2\n");
  123.                 scanf("%d",&j);
  124.  
  125.                 if ( j == 1 )
  126.                     imprime(lista1);
  127.                 else
  128.                     if ( j == 2 )
  129.                         imprime(lista2);
  130.                 break;
  131.  
  132.             case 3:
  133.                 copiar( &lista1, &lista2);
  134.                 break;
  135.         }
  136.     }
  137.     while ( j != 0 );
  138.  
  139.     limpaLista(&lista1);
  140.     limpaLista(&lista2);
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement