Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 KB | None | 0 0
  1.  
  2.  
  3.     #include <stdio.h>
  4.     #include <stdlib.h>
  5.  
  6.     typedef struct no
  7.     {
  8.             int dado;
  9.             struct no *proximo;
  10.             struct no *anterior;     // Imagina aquele quadrado, eh o ponteiro apontado pro endereco do no anterior;
  11.     } tno;
  12.  
  13.     typedef struct lista
  14.     {
  15.             tno *fim;   // ponteiro para o primeiro no da lista;
  16.     } tlista;
  17.  
  18.     void inicia_lista (tlista *l)
  19.     {
  20.         l->fim= NULL;
  21.     }
  22.  
  23.     tno *alocano (int dado)
  24.     {
  25.             tno *p;
  26.             p = malloc(sizeof(tno));
  27.             if(p)
  28.             {
  29.                     p->dado = dado;
  30.                     p->proximo = NULL;
  31.                     p->anterior = NULL;
  32.             }
  33.             return p;
  34.     }
  35.  
  36.     void insereno (tlista *l, int dado)
  37.     {
  38.             tno *aux, *ant, *no;        // tno *ant = eh o no anterior ao novo no que voce ira colocar;
  39.             no = alocano(dado);
  40.             if(no)
  41.             {
  42.                     aux = l->fim->proximo;                       // ******
  43.                     ant = NULL;
  44.                     while(aux && aux->dado < dado)      // Esse while eh para procurar onde vai ser inserido o elemento; se aux == NULL(pode ser porque a lista ta vazia ou) eh porque vai ser o ultimo elemento;
  45.                     {
  46.                             ant = aux;
  47.                             aux = aux->proximo;
  48.                     }
  49.                     if(!ant)
  50.                     {
  51.                             no->proximo = l->fim;
  52.                             if(l->fim){
  53.                                 l->fim->proximo = no;    // Para colocar um novo no no começo da lista;
  54.  
  55.                             }
  56.                             l->fim->proximo = no;
  57.  
  58.                     }
  59.                     else
  60.                     {
  61.                             no->proximo = aux;         // para colocar um elemnto no fim da lista;
  62.                             no->anterior = ant;
  63.                             ant->proximo = no;
  64.                             l->fim = no;                        // ******
  65.                             l->fim->proximo = l->com->anterior; // ******
  66.                             if(aux) aux->anterior = no;  // nao eh o ultimo elemento;
  67.                     }
  68.             }
  69.     }
  70.  
  71.     int retiralista (tlista *l, int dado)
  72.     {
  73.             int r;
  74.             tno *aux, *ant;
  75.             // 1 = removeu
  76.         //  //0 = nao achou
  77.         //  //-1 = lista vazia
  78.             aux = l->com;
  79.             ant = NULL;
  80.             if(!aux) return -1; // lista vazia
  81.             while(aux && aux->dado != dado)
  82.             {
  83.                     ant = aux;
  84.                     aux = aux->proximo;
  85.             }
  86.             if(!aux) return 0; // não achou
  87.             r = aux->dado;
  88.             if(!ant) // !(l->com->ant)
  89.             {
  90.                     if(aux->proximo) aux->proximo->anterior = l->fim->proximo;  // ******
  91.                     l->com = aux->proximo;
  92.                     l->com->anterior = l->com->fim; // ******
  93.             }
  94.             else
  95.             {
  96.                     ant->proximo = aux->proximo;
  97.                     if(aux->proximo) aux->proximo->anterior = aux->anterior;
  98.             }
  99.             free(aux);
  100.             return r;
  101.     }
  102.  
  103.     void mostralista (tlista l)
  104.     {
  105.             tno *aux;
  106.             aux = l.com;
  107.             while(aux)
  108.             {
  109.                     printf("%d ", aux->dado);
  110.                     aux = aux->proximo;
  111.             }
  112.             printf("\n");
  113.     }
  114.  
  115.     void mostralistainvertida (tlista l)
  116.     {
  117.             tno *aux, *ant;
  118.             aux = l.com;
  119.             ant = NULL;
  120.             while(aux)
  121.             {
  122.  
  123.                 if(aux->proximo == NULL){
  124.                     ant = aux;
  125.                     while(ant){
  126.                         printf("%d ",ant->dado);
  127.                         ant = ant->anterior;
  128.                     }
  129.                 }
  130.                 ant = aux;
  131.                 aux = aux->proximo;
  132.             }
  133.  
  134.             printf("\n");
  135.     }
  136.  
  137.     int main()
  138.     {
  139.             tlista l;
  140.             tno no;
  141.             int i, dado, x, y;
  142.             inicia_lista(&l);
  143.             printf("qtde de elementos da lista: ");
  144.             scanf("%d", &x);
  145.             printf("digite os elementos:\n");
  146.             for(i=0; i<x; i++)
  147.             {
  148.                     scanf("%d", &dado);
  149.                     insereno(&l, dado);
  150.             }
  151.             printf("lista: ");
  152.             mostralista(l);
  153.             printf("digite um elemento para retirar: ");
  154.             scanf("%d", &y);
  155.             y = retiralista(&l, y);
  156.             printf("o elemento %d foi retirado da lista.\n", y);
  157.             printf("lista: ");
  158.             mostralista(l);
  159.             printf("lista invertida: ");
  160.             mostralistainvertida(l);
  161.             return 0;
  162.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement