Advertisement
luciana1237

Untitled

Jul 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #define MAX 3
  6.  
  7. typedef struct No {
  8.     int x,v[MAX];
  9.     struct No *prox,*inicio,*fim;
  10. }fila;
  11.  
  12. fila *fila_cria(void)
  13. {
  14.     /* funcao que cria uma fila vazia */
  15.     fila *f = (fila *) malloc(sizeof(fila));
  16.    
  17.     if (f != NULL) /* se nC#o houver nenhuim erro de alocacao */
  18.         f->inicio = NULL;
  19.         f->prox = NULL;
  20.         f->fim = NULL;
  21.        
  22.     return f; /* retorne a fila */
  23.        
  24. }
  25.  
  26. bool isEmpty(fila *f)
  27. {
  28.     return(f->fim == NULL);
  29. }
  30.  
  31. bool enfileirar(fila *f,int v)
  32. {
  33.     fila *no = (fila *) malloc(sizeof(fila)); /* aloque dinamicamente */
  34.     fila *tmp;
  35.    
  36.     if (no == NULL)
  37.         fprintf(stderr,"erro \n"); /* fprintf(2...); envia um fluxo para uma saida */
  38.        
  39.     f->x =v;
  40.     f->prox = NULL;
  41.    
  42.     if (isEmpty(f)){
  43.         f->inicio = no;
  44.         f->x =v;
  45.  
  46.     }else{
  47.         //tmp = no->prox;
  48.         while(tmp != NULL){
  49.             tmp = tmp->prox;
  50.             f->x =v;
  51.             f->fim->prox = no;
  52.         }
  53.         f->fim->prox = no;
  54.     }
  55.     return true;
  56. }
  57.  
  58. void imprime(fila *f)
  59. {
  60.     fila *pointer;
  61.     pointer = f;
  62.     int i;
  63.     for(; pointer != NULL; pointer = pointer->prox)
  64.         printf(" [%d]  \n",pointer->x);
  65. }
  66. int main()
  67. {
  68.     fila *f= fila_cria();
  69.     int opc,num;
  70.     for(;;){
  71.         printf("1---inserir \n");
  72.         printf("2--- imprime \n");
  73.         scanf("%d ",&opc);
  74.  
  75.         switch(opc){
  76.             case 1:
  77.                 scanf("%d",&num);
  78.                 enfileirar(f,num);
  79.                 break;
  80.             case 2:
  81.                 imprime(f);
  82.                 break;
  83.             default:
  84.                 break;
  85.         }
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement