Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct LISTA
- {
- int info;
- struct LISTA *prox;
- };
- void limpaLista( struct LISTA **lista)
- {
- if( (*lista)->prox != NULL )
- limpaLista( &(*lista)->prox );
- free(*lista);
- *lista = NULL;
- }
- void addItem (struct LISTA **lista)
- {
- struct LISTA *atual = NULL;
- atual = *lista;
- system("cls");
- printf("Digite a informacao\n");
- if ( *lista == NULL )
- {
- *lista = malloc( sizeof(struct LISTA) );
- atual = *lista;
- }
- else
- {
- while( atual->prox != NULL )
- atual = atual->prox ;
- atual->prox = malloc( sizeof(struct LISTA) );
- atual = atual->prox;
- }
- system("cls");
- printf("Digite a informacao\n");
- scanf("%d",&atual->info);
- atual->prox = NULL;
- }
- void imprime(struct LISTA *lista)
- {
- system("cls");
- struct LISTA *p;
- printf("Elementos: \n");
- for (p = lista; p != NULL; p = p->prox)
- {
- printf( " * %d\n",p->info);
- }
- }
- struct LISTA * inicializa(void)
- {
- return NULL;
- }
- void copiar (struct LISTA **lista1, struct LISTA **lista2)
- {
- struct LISTA *p = *lista1;
- struct LISTA *q = *lista2;
- limpaLista( lista2 ); // Esvazia lista 2
- for (; p != NULL; p = p->prox ) // Percorre lista 1
- {
- if ( *lista2 == NULL ) // Se a lista estiver vazia, recebe o primeiro elemento
- {
- *lista2 = malloc( sizeof(struct LISTA) );
- (*lista2)->info = p->info;
- (*lista2)->prox = NULL;
- }
- else
- {
- q->prox = malloc( sizeof(struct LISTA) ); // Cria elemento na posição vazia
- q = q->prox; // Desloca para a posição do novo elemento
- q->info = p->info; // Recebe valor da lista 1
- q->prox = NULL;
- }
- }
- printf("Copia completa\n");
- }
- int main(void)
- {
- int j = 0;
- struct LISTA *lista1 = NULL;
- struct LISTA *lista2 = NULL;
- do
- {
- printf("Escolha uma das funcoes\n");
- 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");
- scanf("%d",&j);
- switch (j)
- {
- case 1:
- printf("Escolha se voce quer inserir na LISTA 1 ou na LISTA 2\n");
- scanf("%d",&j);
- if ( j == 1 )
- addItem(&lista1);
- else
- if ( j == 2 )
- addItem(&lista2);
- break;
- case 2:
- printf("Escolha se voce quer imprimir a LISTA 1 ou a LISTA 2\n");
- scanf("%d",&j);
- if ( j == 1 )
- imprime(lista1);
- else
- if ( j == 2 )
- imprime(lista2);
- break;
- case 3:
- copiar( &lista1, &lista2);
- break;
- }
- }
- while ( j != 0 );
- limpaLista(&lista1);
- limpaLista(&lista2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement