Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct lista{
  5.     int valor;
  6.     struct lista* prox;
  7.     struct lista* ant;
  8. };
  9. typedef struct lista Lista;
  10.  
  11. Lista* insere(Lista* lista, int val){
  12.     Lista* novo = (Lista*) malloc(sizeof(Lista));
  13.     novo->valor = val;
  14.     novo->prox = lista;
  15.     novo->ant = NULL;
  16.     if(lista != NULL){
  17.         lista->ant = novo;
  18.         return novo;
  19.     }
  20. }
  21.  
  22. Lista* duplica(Lista* lista){
  23.     Lista* p = lista;
  24.     if(p == NULL){
  25.         return 0;
  26.     }else{
  27.         Lista* novo = (Lista*) malloc(sizeof(Lista));
  28.         novo->valor = p->valor;
  29.         novo->prox = p->prox;
  30.         novo->ant = p;
  31.         p->prox = novo;
  32.         p->prox->ant = novo;
  33.         duplica(novo->prox);
  34.         return p;
  35.     }
  36. }
  37.  
  38. void printavalor(Lista* lista){
  39.     Lista* p = lista;
  40.     while(p != NULL){
  41.         printf("%d ", p->valor);
  42.         p = p->prox;
  43.     }
  44.     return;
  45. }
  46.  
  47. main(){
  48.     Lista* lista = NULL;
  49.     lista = insere(lista, 4);
  50.     lista = insere(lista, 7);
  51.     lista = insere(lista, 1);
  52.     printf("------ VALORES -----\n");
  53.     printavalor(lista);
  54.     printf("\n\n\n");
  55.     lista = duplica(lista);
  56.     printf("-- VALORES DUPLOS --\n");
  57.     printavalor(lista);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement