Advertisement
luciana1237

Untitled

Jul 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 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],size;
  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. cel *cria_cel(void)
  25. {
  26.     cel *c = (cel *) malloc(sizeof(cel));
  27.    
  28.     if (c!= NULL)
  29.         c->ant = NULL;
  30.         c->prox = NULL;
  31.     return c;
  32. }
  33.  
  34. bool isEmpty(cel *c)
  35. {
  36.     return (c->prox == NULL);
  37. }
  38.  
  39. bool Push(cel **c,pessoa *p)
  40. {
  41.     cel *novo = (cel *) malloc(sizeof(cel));
  42.     cel *aux;
  43.    
  44.     if (novo == NULL)
  45.     {
  46.         fprintf(stderr," erro "); exit( 0 );
  47.     }
  48.     novo->dados = p;
  49.     novo->prox = NULL;
  50.     novo->ant = (*c);
  51.    
  52.    
  53.     if (isEmpty(*c)){
  54.         printf("vazia");
  55.         (*c)->prox = novo;
  56.     }else{
  57.         printf("tem");
  58.         aux = (*c)->prox;
  59.        
  60.         while(aux->prox != NULL){
  61.             aux = aux->prox;
  62.         }
  63.         aux->prox = novo;
  64.         aux->ant = aux;
  65.        
  66.     }
  67.     return true;
  68. }
  69.  
  70. void ShowList(cel **c)
  71. {
  72.     cel *pointer;
  73.     pointer = (*c)->prox;
  74.     for(; pointer != NULL; pointer = pointer->prox)
  75.         printf("\n %s \n",pointer->dados->str);
  76. }
  77.  
  78. int sizeList(cel **c)
  79. {
  80.     int count=0;
  81.    
  82.     cel *pointer = (*c)->prox;
  83.    
  84.     for(; pointer != NULL; pointer =pointer->prox)
  85.         count++;
  86.    
  87.     return count;
  88. }
  89.  
  90.  
  91. void libera_lista(cel **c)
  92. {
  93.     cel *no;
  94.     if (!isEmpty(*c)){
  95.         while((*c)->prox != NULL){
  96.             no = (*c);
  97.             (*c) = (*c)->prox;
  98.            
  99.             free(no);
  100.         }
  101.     }else {
  102.         printf("lista vazia\n");
  103.     }
  104. }
  105.  
  106.  
  107. int main()
  108. {
  109.     int tam_v,_opc,elem;
  110.     pessoa p;
  111.     strcpy(p.str,"abc");
  112.     cel *c = cria_cel();
  113.     //Push(&c,&p);
  114.     //imprime(&c);
  115.    
  116.     for(;;){
  117.        
  118.         printf("\n\t(0)---------- SAIR \n");
  119.         printf("\n\t(1)---------- INSERIR ELEMENTO NA LISTA (PUSH) \n");
  120.         printf("\n\t(2)---------- REMOVER ELEMENTO DA LISTA (POP) \n");
  121.         printf("\n\t(3)---------- IMPRIMIR \n");
  122.         printf("\n\t(4)---------- TAMANHO DA LISTA \n");
  123.         printf("\n\t(5)---------- LIBERAR LISTA \n");
  124.        
  125.         printf("> "); scanf("%d",&_opc);
  126.        
  127.         switch(_opc){
  128.             case 0:
  129.                 exit( 0 );
  130.             default:
  131.                 fprintf(stdout,"[-] COMANDO INVALIDO!!! [-]");
  132.                 break;
  133.             case 1:
  134.                 printf(" Digite (PUSH):\n");
  135.                 printf("> "); scanf("%d",&elem);
  136.                 break;
  137.             case 2:
  138.                 printf("\n POP \n");
  139.                 beak;
  140.             case 3:
  141.                 ShowList(&c);
  142.                 break;
  143.             case 4:
  144.                 tam_v = SizeList(&c);
  145.                 printf("\n TAMANHO DA LISTA -> [ %d ]\n",tam_v);
  146.                 break;
  147.             case 5:
  148.                 libera_lsita(&c);
  149.                 break;
  150.                
  151.                
  152.         }
  153.        
  154.     }
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement