Advertisement
luciana1237

Untitled

Jul 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 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.     pessoa *dados;
  18.     struct Celula *ant;
  19.     struct Celula *prox;
  20.    
  21. }cel;
  22.  
  23. cel *cria_cel(void)
  24. {
  25.     cel *c = (cel *) malloc(sizeof(cel));
  26.    
  27.     if (c!= NULL)
  28.         c->ant = NULL;
  29.         c->prox = NULL;
  30.     return c;
  31. }
  32.  
  33. bool isEmpty(cel *c)
  34. {
  35.     return (c->prox == NULL);
  36. }
  37.  
  38. bool Push(cel **c,pessoa *p)
  39. {
  40.     cel *novo = (cel *) malloc(sizeof(cel));
  41.     cel *aux;
  42.    
  43.     if (novo == NULL)
  44.     {
  45.         fprintf(stderr," erro "); exit( 0 );
  46.     }
  47.     novo->dados = p;
  48.     novo->prox = NULL;
  49.     novo->ant = (*c);
  50.    
  51.    
  52.     if (isEmpty(*c)){
  53.         printf("vazia");
  54.         (*c)->prox = novo;
  55.     }else{
  56.         printf("tem");
  57.         aux = (*c)->prox;
  58.         while(aux->prox != NULL){
  59.             aux = aux->prox;
  60.         }
  61.         aux->prox = novo;
  62.         aux->ant = aux;
  63.        
  64.     }
  65.     return true;
  66. }
  67.  
  68. void imprime(cel **c)
  69. {
  70.     cel *pointer;
  71.     pointer = (*c)->prox;
  72.     for(; pointer != NULL; pointer = pointer->prox)
  73.         printf("\n %s \n",pointer->dados->str);
  74. }
  75.  
  76. void Pop(cel **c)
  77. {
  78.     if (!isEmpty(*c)){
  79.         cel *no;
  80.         no = (*c);
  81.        
  82.         free(no);
  83.     }
  84. }
  85.  
  86. void libera_lista(cel **c)
  87. {
  88.     cel *no;
  89.     if (!isEmpty(*c)){
  90.         while((*c)->prox != NULL){
  91.             no = (*c);
  92.             (*c) = (*c)->prox;
  93.            
  94.             free(no);
  95.         }
  96.     }else {
  97.         printf("lista vazia\n");
  98.     }
  99. }
  100. int main()
  101. {
  102.     pessoa p;
  103.     strcpy(p.str,"abc");
  104.  
  105.     int tam;
  106.     cel *c = cria_cel();
  107.     Push(&c,&p);
  108.     Push(&c,&p);
  109.     //imprime(&c);
  110.     Pop(&c);
  111.     //libera_lista(&c);
  112.     imprime(&c);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement