Advertisement
SergioRP

Lista Dinamica Simples

Sep 13th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //Declaracao da Estrutura
  5.  
  6. struct tipoElemento{ //criar funcao
  7.     int valor;
  8.     struct tipoElemento *proximo;
  9. };
  10.  
  11. struct tipoLista{
  12.     struct tipoElemento *inicio;
  13.     struct tipoElemento *fim;
  14. };
  15.  
  16. //---------------------------------------------------------------
  17. // ASSINATURAS DAS FUNÇÕES
  18. //---------------------------------------------------------------
  19. void inicializar(struct tipoLista *lista);
  20. void inserir_inicio(struct tipoLista *lista, struct tipoElemento *elem);
  21. void inserir_fim(struct tipoLista *lista, struct tipoElemento *elem);
  22. void remover_inicio(struct tipoLista *lista);
  23. void remover_fim(struct tipoLista *lista);
  24. void listar(struct tipoLista *lista);
  25. void pesquisar(struct tipoLista *lista, struct tipoElemento *elem);
  26. int lista_vazia(struct tipoLista *lista);
  27. int menu();
  28. struct tipoElemento *lerElemento();
  29.  
  30.  
  31. //------------------------------------------
  32. //PROGRAMA PRINCIPAL
  33. //------------------------------------------
  34.  
  35.  
  36. int main(){
  37.     struct tipoLista lista;
  38.     struct tipoElemento *elem;
  39.     int opcao;
  40.  
  41.     inicializar(&lista);
  42.      do{
  43.         system("cls");
  44.         opcao = menu();
  45.  
  46.         system("cls");
  47.         switch(opcao)
  48.         {
  49.             case 1:
  50.                 printf("INSERIR INICIO \n\n");
  51.                 elem = lerElemento();
  52.                 inserir_inicio(&lista, elem);
  53.                 break;
  54.             case 2:
  55.                 printf("INSERIR FIM \n\n");
  56.                 elem = lerElemento();
  57.                 inserir_fim(&lista, elem); //nao esquecer desta merda
  58.                 break;
  59.             case 3:
  60.                 printf("REMOVER INICIO \n\n");
  61.                 remover_inicio(&lista);
  62.                 break;
  63.             case 4:
  64.                 printf("REMOVER FIM \n\n");
  65.                 remover_fim(&lista);
  66.                 break;
  67.             case 5:
  68.                 printf("PESQUISAR \n\n");
  69.                 break;
  70.             case 6:
  71.                 printf("LISTAR \n\n");
  72.                 listar(&lista);
  73.                 break;
  74.  
  75.  
  76.         }
  77.         system("pause");
  78. }while(opcao !=0);
  79. return 0;
  80. }
  81. void inicializar(struct tipoLista *lista){
  82. lista->inicio = NULL;
  83. lista->fim = NULL;
  84. }
  85.  
  86. void inserir_inicio(struct tipoLista *lista, struct tipoElemento *elem){
  87.  
  88.     if(lista_vazia(lista) == 1){
  89.         lista->inicio = elem;
  90.         lista->fim = elem;
  91.     }else{
  92.         elem->proximo = lista->inicio; //proximo aponta para o inicio
  93.         lista->inicio = elem;
  94.  
  95.     }
  96. }
  97.  
  98. void inserir_fim(struct tipoLista *lista, struct tipoElemento *elem){
  99.     if(lista_vazia(lista) == 1){
  100.         lista->inicio = elem;
  101.         lista->fim = elem;
  102.     } else{
  103.         lista->fim->proximo = elem;
  104.         lista->fim = elem;
  105.     }
  106. }
  107.  
  108. void remover_inicio(struct tipoLista *lista) {
  109.     if (lista->inicio == lista->fim) {
  110.         lista->inicio = NULL;
  111.         lista->fim = NULL;
  112.     } else {
  113.         lista->inicio = lista->inicio->proximo;
  114.     }
  115. }
  116.  
  117. void remover_fim(struct tipoLista *lista) {
  118.     if (lista->inicio == lista->fim) {
  119.         lista->inicio = NULL;
  120.         lista->fim = NULL;
  121.     } else {
  122.         struct tipoElemento *aux = lista->inicio;
  123.         while(aux->proximo != lista->fim)
  124.             aux = aux->proximo;
  125.         lista->fim = aux;
  126.         lista->fim->proximo = NULL;
  127.     }
  128. }
  129.  
  130. void listar(struct tipoLista *lista){
  131.  
  132.     if(lista_vazia(lista) == 1){
  133.         printf("\n\nA lista esta VAZIA!\n\n");
  134.     } else{
  135.     struct tipoElemento *aux = lista->inicio;
  136.  
  137.     while(aux!= NULL){
  138.         printf("\nValor: %d", aux->valor);
  139.         aux = aux->proximo;
  140.         }
  141.         printf("\nFIM\n\n");
  142.     }
  143.  
  144. }
  145.  
  146. int lista_vazia(struct tipoLista *lista){
  147.     if(lista->inicio == NULL && lista -> fim == NULL){
  148.         return 1; //SIM, a lista esta vazia
  149.     }else{
  150.         return 0;
  151.     }   //NAO, a lista nao esta vazia
  152.  
  153. }
  154.  
  155.  
  156. //------------------------------------------
  157. //-----------MENU------------
  158.  
  159. int menu()
  160. {
  161.     int opcao = 0;
  162.     printf("\n===================================");
  163.     printf("\n     LISTA DINAMICA SIMPLES     ");
  164.     printf("\n===================================");
  165.     printf("\n [1] Inserir  Inicio   ");
  166.     printf("\n [2] Inserir  Fim   ");
  167.     printf("\n [3] Remover Inicio   ");
  168.     printf("\n [4] Remover Fim   ");
  169.     printf("\n [5] Pesquisar ");
  170.     printf("\n [6] Listar    ");
  171.     printf("\n [0] Sair      ");
  172.     printf("\n\n Opcao: ");
  173.     scanf("%d",&opcao);
  174.     return opcao;
  175. }
  176.  
  177. //-------------------------------
  178. struct tipoElemento *lerElemento(){
  179.  
  180.     struct tipoElemento *novo;
  181.  
  182.     //Alocacao de memoria
  183.     /*A funcao malloc eh utilizada para a alocacao de memoria. O nome
  184.     eh uma abreviatura de memory allocation.
  185.     A alocacao de memoria reserva um espaco na memoria principal (RAM)
  186.     para manipulacao de dados. */
  187.     //malloc(1000) -> aloca 1000 bytes
  188.     novo = (struct tipoElemento *)malloc(sizeof(struct tipoElemento));
  189.  
  190.     //Leitura de valor. Tem que ter a funcao malloc. Ver no gerenciador
  191.     printf("\nValor: ");
  192.     scanf("%d", &novo->valor);
  193.     novo->proximo = NULL;
  194.  
  195.     return novo;
  196.  
  197. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement