PedroHMM

LISTALIGADADINAMICASENTINELA

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