PedroHMM

Filas

Apr 30th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<malloc.h>
  5.  
  6. typedef struct estrutura{
  7.         int valor;
  8.         estrutura *prox;
  9. }NO;
  10.  
  11. typedef struct
  12. {
  13.         NO* inicio;
  14.         NO* fim;
  15. }FILA;
  16.  
  17. void inicializar(FILA *f)
  18. {
  19.      f->inicio = NULL;
  20.      f->fim = NULL;
  21. }
  22.  
  23. bool filavazia(FILA *f)
  24. {
  25.      if(!f->inicio) return true;
  26.      return false;
  27. }
  28.  
  29. void inserir(FILA *f, int valor)
  30. {
  31.      NO* novo =(NO*) malloc(sizeof(NO));
  32.      novo->valor = valor;
  33.      novo->prox = NULL;
  34.      if(f->fim !=NULL){
  35.                f->fim->prox = novo; // anterior
  36.      }
  37.      else
  38.      {
  39.           f->inicio = novo;  
  40.      }
  41.     f->fim = novo;
  42. }
  43.  
  44. int retirar(FILA *f){
  45.     if(filavazia(f)) return -1;
  46.     int resp = f->inicio->valor;
  47.     NO* aux = f->inicio;
  48.     f->inicio = f->inicio->prox;
  49.     free(aux);
  50.     if(!f->inicio) f->fim =NULL;
  51.     return resp;
  52. }
  53.  
  54. void imprimir(FILA *f)
  55. {
  56.      NO* p = f->inicio;
  57.      while(p)
  58.      {
  59.              printf("%i\t", p->valor);
  60.              p = p->prox;
  61.      }
  62.      printf("\n");
  63. }
  64.  
  65. main()
  66. {
  67.      
  68.       FILA f;
  69.       inicializar(&f);
  70.      
  71.       for(int i=0; i<10; i++)
  72.       {
  73.               inserir(&f,i);
  74.               imprimir(&f);
  75.       }
  76.               imprimir(&f);
  77.        for(int i=0; i<10; i++)
  78.       {
  79.               retirar(&f);
  80.               imprimir(&f);
  81.       }
  82.       getch();
  83.      
  84. }
Advertisement
Add Comment
Please, Sign In to add comment