Advertisement
Jonas_3k

/*Alocação @ Lista inserção ordenada -*/

Apr 7th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include<stdio.h>
  2. struct cel
  3. {
  4.     int dado;
  5.     struct cel *NEXT;
  6. };
  7.  
  8. typedef struct cel cel;
  9.  
  10. cel *Penultimo(cel**);
  11. cel *Novo(void);
  12. void Adiciona(cel**,cel**);
  13. void ver(cel*);
  14. void clrscr(void);
  15. char *Descarrega(cel**);
  16. void Sair(cel**);
  17. //void *Deleta(cel**);
  18. //void varrer(cel**);
  19. int main()
  20. {
  21.     cel *dado,*fim;
  22.     fim = NULL;
  23.     dado = NULL;
  24.     char opc,*resp;
  25.     do
  26.     {
  27.         clrscr();
  28.         printf("Escolha:\t[I]nserir\t[V]er\t\033[37m\033[40m[R]emover\033[0;0m\t[S]air\n# ");
  29.         scanf("%c",&opc);
  30.         switch(opc)
  31.         {
  32.             case 'i':
  33.             case 'I': Adiciona(&dado,&fim);
  34.             case 'v':
  35.             case 'V': ver(dado); break;
  36.             case 'r':
  37.             case 'R': break;
  38.             case 's':
  39.             case 'S': Sair(&dado); return 0;
  40.         }
  41.  
  42.  
  43.     }while(1);
  44.  
  45.     return 1;
  46.  
  47. }
  48.  
  49. cel *Penultimo(cel **dado)
  50. {
  51.     if((*dado)->NEXT != NULL)
  52.     {
  53.         Penultimo(&(*dado)->NEXT);
  54.     }
  55.     else return (*dado);
  56. }
  57.  
  58. cel *Novo()
  59. {
  60.     #include<stdlib.h>
  61.     cel *dado;
  62.  
  63.     dado = (cel *) malloc(sizeof(cel));
  64.  
  65.     printf("Valor: ");
  66.  
  67.     scanf("%d",&dado->dado);
  68.  
  69.     dado->NEXT = NULL;
  70.  
  71.     return dado;
  72. }
  73.  
  74. /*void Adiciona(cel **dado)
  75. {
  76.     if(!(*dado))
  77.     {
  78.         (*dado) = Novo();
  79.     }
  80.     else if((*dado)->NEXT != NULL)
  81.     {
  82.         Adiciona(&(*dado)->NEXT);
  83.     }
  84.     else
  85.     {
  86.         (*dado)->NEXT = Novo();
  87.     }*/
  88.  
  89. void Adiciona(cel **inicio,cel **fim)
  90. {
  91.     cel *novo;
  92.     novo = Novo();
  93.     if(!(*inicio))
  94.     {
  95.         (*inicio) = novo;
  96.         (*fim) = (*inicio);
  97.     }
  98.     else
  99.     {
  100.         (*fim) = Penultimo(&(*inicio));
  101.         if(novo->dado < (*inicio)->dado)
  102.         {
  103.             novo->NEXT = (*inicio);
  104.             (*inicio)  = novo;
  105.         }
  106.         else if(novo->dado > (*fim)->dado)
  107.         {
  108.             (*fim)->NEXT = novo;
  109.         }
  110.         else
  111.         {
  112.             cel *anterior,*aux;
  113.             aux = (*inicio);
  114.             while(novo->dado > aux->dado)
  115.             {
  116.                 anterior = aux;
  117.                 aux = aux->NEXT;
  118.             }
  119.             anterior->NEXT = novo;
  120.             novo->NEXT = aux;
  121.         }
  122.     }
  123.     return;
  124.  
  125. }
  126.  
  127. //    return;
  128. //}
  129.  
  130. void  ver(cel *dado)
  131. {
  132.     if(dado != NULL)
  133.     {
  134.         printf("%d\t",dado->dado);
  135.         ver(dado->NEXT);
  136.     }
  137.     scanf("%*c");
  138.     return;
  139. }
  140.  
  141.  
  142. #include<stdlib.h>
  143.  
  144. void clrscr()
  145. {
  146.     #ifdef WIN32
  147.         system("cls");
  148.     #else
  149.         system("clear");
  150.     #endif
  151. }
  152.  
  153. char *Descarrega(cel **inicio)
  154. {
  155.     cel *aux;
  156.     if((*inicio)!= NULL)
  157.     {
  158.         putchar('.');
  159.         aux = (*inicio);
  160.         (*inicio) = (*inicio)->NEXT;
  161.         free(aux);
  162.         Descarrega(&(*inicio));
  163.     }
  164.     else
  165.     {
  166.        return "\nSucesso!";
  167.     }
  168.     return;
  169. }
  170.  
  171. void Sair(cel **inicio)
  172. {
  173.     printf("Saindo");
  174.     printf("%s",Descarrega(&(*inicio)));
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement