Advertisement
luciana1237

Untitled

Jul 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. #define MAX 10
  7.  
  8. struct Pessoa {
  9.     int vetor[MAX],x,v[MAX];
  10.     char str[MAX];
  11.  
  12. };
  13.  
  14. typedef struct Pessoa pessoa;
  15.  
  16. typedef struct Celula {
  17.    
  18.     pessoa *dados;
  19.     struct Celula *ant;
  20.     struct Celula *prox;
  21.    
  22. }cel;
  23.  
  24. int size =0x0;
  25.  
  26. cel *cria_cel(void)
  27. {
  28.     cel *c = (cel *) malloc(sizeof(cel));
  29.    
  30.     if (c!= NULL)
  31.         c->ant = NULL;
  32.         c->prox = NULL;
  33.     return c;
  34. }
  35.  
  36. bool isEmpty(cel *c)
  37. {
  38.     return (c->prox == NULL);
  39. }
  40.  
  41. bool Push(cel **c,pessoa *p)
  42. {
  43.     cel *novo = (cel *) malloc(sizeof(cel));
  44.     cel *aux;
  45.    
  46.     if (novo == NULL)
  47.     {
  48.         fprintf(stderr," erro "); exit( 0 );
  49.     }
  50.     novo->dados = p;
  51.     novo->prox = NULL;
  52.     novo->ant = (*c);
  53.    
  54.    
  55.     if (isEmpty(*c)){
  56.         printf("vazia");
  57.         (*c)->prox = novo;
  58.     }else{
  59.         printf("tem");
  60.         aux = (*c)->prox;
  61.        
  62.         while(aux->prox != NULL){
  63.             aux = aux->prox;
  64.         }
  65.         aux->prox = novo;
  66.         aux->ant = aux;
  67.        
  68.     }
  69.     size++;
  70.     return true;
  71. }
  72.  
  73. void ShowList(cel **c)
  74. {
  75.     size_t count=0;
  76.  
  77.     cel *pointer;
  78.     pointer = (*c)->prox;
  79.  
  80.     if(isEmpty(*c))
  81.         printf("----LISTA VAZIA----");
  82.     if (!isEmpty(*c))
  83.         for(; count <= size; count++)
  84.             printf("%d \n",pointer->dados->vetor[count]);
  85. }
  86.  
  87. int sizeList(cel **c)
  88. {
  89.     size_t count = 0;
  90.  
  91.     if (!isEmpty(*c)){
  92.         cel *pointer = (*c)->prox;
  93.         for(; pointer != NULL; pointer =pointer->prox)
  94.             count++;
  95.     }if (isEmpty(*c))
  96.         printf("----LISTA VAZIA----");
  97.  
  98.  
  99.     return count;
  100. }
  101.  
  102.  
  103. void libera_lista(cel **c)
  104. {
  105.     cel *no;
  106.     if (!isEmpty(*c)){
  107.         while((*c)->prox != NULL){
  108.             no = (*c);
  109.             (*c) = (*c)->prox;
  110.            
  111.             free(no);
  112.             size--;
  113.         }
  114.     }if(isEmpty(*c))
  115.         printf("\n ----LISTA VAZIA----\n");
  116. }
  117.  
  118. bool SearchList(cel **c,pessoa *_elem)
  119. {
  120.     cel *pointer=(*c)->prox;
  121.     cel *aux;
  122.     int i=0;
  123.    
  124.    
  125.    
  126.     if (!isEmpty(*c))
  127.         for(; i <=size; i++){
  128.             if (pointer->dados->vetor[i] == *_elem){
  129.                 printf("\nENCONTRADO \n");
  130.             }else{
  131.                 printf("\nnao encontrado\n");
  132.             }
  133.         }
  134.     if(isEmpty(*c))
  135.         fprintf(stdout,"----LISTA VAZIA----");
  136.         return false;
  137.    
  138. }
  139.  
  140.  
  141. int main()
  142. {
  143.  
  144.     int _opc,elem,pos,tam_v;
  145.     pessoa p;
  146.  
  147.     cel *c = cria_cel();
  148.  
  149.     for(;;){
  150.        
  151.         printf("\n\t(0)---------- SAIR ");
  152.         printf("\n\t(1)---------- INSERIR ELEMENTO NO INICIO DA LISTA (PUSH) ");
  153.         printf("\n\t(2)---------- REMOVER ELEMENTO DA LISTA (POP) ");
  154.         printf("\n\t(3)---------- IMPRIMIR ");
  155.         printf("\n\t(4)---------- TAMANHO DA LISTA ");
  156.         printf("\n\t(5)---------- BUSCAR LISTA (SEARCH) ");
  157.         printf("\n\t(6)---------- LIBERAR LISTA \n");
  158.        
  159.         printf("> "); scanf("%d",&_opc);
  160.        
  161.         switch(_opc){
  162.             case 0:
  163.                 exit( 0 );
  164.                // break;
  165.             default:
  166.                 fprintf(stdout,"[-] COMANDO INVALIDO!!! [-]");
  167.                 break;
  168.             case 1:
  169.                 printf(" Digite (PUSH) e a posicao do vetor :\n");
  170.                 printf("> "); scanf("%d %d",&elem,&pos);
  171.                 p.vetor[pos] =elem;
  172.                 Push(&c,&p);
  173.                 break;
  174.             case 2:
  175.                 printf("\n POP \n");
  176.                 break;
  177.             case 3:
  178.                 ShowList(&c);
  179.                 break;
  180.             case 4:
  181.                 tam_v = sizeList(&c);
  182.                 printf("\n TAMANHO DA LISTA -> [ %d ]\n",tam_v);
  183.                 break;
  184.             case 5:
  185.                 printf("Digite o item de busca: ");
  186.                 scanf("%d",&elem);
  187.                 p.x = elem;
  188.                 SearchList(&c,&p);
  189.                 break;
  190.             case 6:
  191.                 libera_lista(&c);
  192.                 break;
  193.         }
  194.        
  195.     }
  196.     return 0;
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement