PedroHMM

LISTALIGADADINAMICA

Apr 4th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5.  
  6. typedef struct estrutura
  7. {
  8.         int chave;
  9.         estrutura *prox;
  10. } NO;
  11.  
  12. typedef struct
  13. {
  14.         NO* inicio;
  15. } LISTA;
  16.  
  17. void inicializar(LISTA *l)
  18. {
  19.      l->inicio = NULL;
  20. }
  21.  
  22. void exibir(LISTA *l)
  23. {
  24.      NO* p = l->inicio;
  25.      while(p)
  26.      {
  27.              printf("%i\t", p->chave);
  28.              p = p->prox;
  29.      }
  30.      printf("\n");
  31. }
  32.  
  33. NO* buscar(LISTA *l, int chave, NO* *ant)
  34. {
  35.     NO* p = l->inicio;
  36.     *ant = NULL;
  37.     while(p)
  38.     {
  39.             if(p->chave == chave) return p;
  40.             *ant = p;
  41.             p = p->prox;
  42.     }
  43.     return NULL;
  44. }
  45.  
  46. bool anexar(LISTA *l, int chave)
  47. {
  48.      NO* ant;
  49.      NO* novo = buscar(l, chave, &ant);
  50.      if(novo) return false;
  51.      novo = (NO*) malloc(sizeof(NO));
  52.      novo->chave = chave;
  53.      if(!l->inicio)
  54.      {
  55.                    l->inicio = novo;
  56.                    novo->prox = NULL;
  57.      }
  58.      else
  59.      {
  60.          if(!ant)
  61.          {
  62.                  novo->prox = l->inicio;
  63.                  l->inicio = novo;
  64.          }
  65.          else
  66.          {
  67.                  novo->prox = ant->prox;
  68.                  ant->prox = novo;  
  69.          }
  70.      }
  71.      return true;
  72. }
  73.  
  74. int size(LISTA *l)
  75. {
  76.     NO* p = l->inicio;
  77.     int cont = 0;
  78.     while(p)
  79.     {
  80.             cont++;
  81.             p = p->prox;
  82.     }
  83.     return cont;
  84. }
  85.  
  86. bool inserir(LISTA *l, int chave, int pos)
  87. {
  88.      if(pos <0) return false;
  89.      NO* ant;
  90.      NO* novo = buscar(l, chave, &ant);
  91.      if(novo) return false;
  92.       novo = (NO*) malloc(sizeof(NO));
  93.       novo->chave =chave;
  94.      if(pos == 0)
  95.      {
  96.             novo->prox = l->inicio;
  97.             l->inicio = novo;
  98.      }
  99.      if(pos >= size(l))
  100.      {
  101.             ant->prox = novo;
  102.             novo->prox = NULL;
  103.      }
  104.      else
  105.          {
  106.              int cont=0;
  107.              NO* p = l->inicio;
  108.              while(p)
  109.              {
  110.                      if(cont == pos -1)
  111.                      {
  112.                              novo->prox = p->prox;
  113.                              p->prox =novo;
  114.                              return true;
  115.                      }
  116.                      else
  117.                      {
  118.                          cont ++;
  119.                          p =p->prox;
  120.                      }
  121.              }        
  122.          }
  123.          return true;
  124. }
  125.  
  126. bool excluir(LISTA *l, int chave)
  127. {
  128.      NO* ant;
  129.      NO* p = buscar(l, chave, &ant);
  130.      if(!p) return false;
  131.      if(!ant) l->inicio = p->prox;
  132.      else ant->prox = p->prox;
  133.      free(p);
  134.      return true;
  135. }
  136.  
  137. void destruir(LISTA *l)
  138. {
  139.      NO* p =l->inicio;
  140.      NO* aux;
  141.      while(p)
  142.      {
  143.              aux = p->prox;
  144.              free(p);
  145.              p = aux;
  146.      }
  147.      l->inicio = NULL;
  148. }
  149.  
  150. main()
  151. {
  152.       LISTA l;
  153.       inicializar(&l);
  154.       anexar(&l, 0);
  155.       anexar(&l, 1);
  156.       anexar(&l, 2);
  157.       anexar(&l, 3);
  158.       anexar(&l, 4);
  159.       exibir(&l);
  160.       getch();
  161.       inserir(&l, 80, 3);
  162.       exibir(&l);
  163.       getch();
  164.       inserir(&l, 50, 0);
  165.       exibir(&l);
  166.       getch();
  167.       inserir(&l, 100, 100000000);
  168.       exibir(&l);
  169.       getch();
  170.       inserir(&l, 2000, 55555);
  171.       exibir(&l);
  172.       getch();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment