guest_01

fila.c

Apr 7th, 2021 (edited)
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node{
  5.  int num;
  6.  struct Node *prox;
  7. };
  8. typedef struct Node node;
  9.  
  10. int tam;
  11.  
  12. int menu(void);
  13. void opcao(node *FILA, int op);
  14. void inicia(node *FILA);
  15. int vazia(node *FILA);
  16. node *aloca();
  17. void insere(node *FILA);
  18. node *retira(node *FILA);
  19. void exibe(node *FILA);
  20. void libera(node *FILA);
  21.  
  22.  
  23. int main(void)
  24. {
  25.  node *FILA = (node *) malloc(sizeof(node));
  26.  if(!FILA){
  27.   printf("Sem memoria disponivel!\n");
  28.   exit(1);
  29.  }else{
  30.  inicia(FILA);
  31.  int opt;
  32.  
  33.  do{
  34.   opt=menu();
  35.   opcao(FILA,opt);
  36.  }while(opt);
  37.  
  38.  free(FILA);
  39.  return 0;
  40.  }
  41. }
  42.  
  43.  
  44. int menu(void)
  45. {
  46.  int opt;
  47.  
  48.  printf("Escolha a opcao\n");
  49.  printf("0. Sair\n");
  50.  printf("1. Zerar fila\n");
  51.  printf("2. Exibir fila\n");
  52.  printf("3. Adicionar Elemento na Fila\n");
  53.  printf("4. Retirar Elemento da Fila\n");
  54.  printf("Opcao: "); scanf("%d", &opt);
  55.  
  56.  return opt;
  57. }
  58.  
  59. void opcao(node *FILA, int op)
  60. {
  61.  node *tmp;
  62.  switch(op){
  63.   case 0:
  64.    libera(FILA);
  65.    break;
  66.  
  67.   case 1:
  68.    libera(FILA);
  69.    inicia(FILA);
  70.    break;
  71.  
  72.   case 2:
  73.    exibe(FILA);
  74.    break;
  75.  
  76.   case 3:
  77.    insere(FILA);
  78.    break;
  79.  
  80.   case 4:
  81.    tmp= retira(FILA);
  82.    if(tmp != NULL){
  83.     printf("Retirado: %3d\n\n", tmp->num);
  84.     libera(tmp);
  85.    }
  86.    break;
  87.  
  88.   default:
  89.    printf("Comando invalido\n\n");
  90.  }
  91. }
  92.  
  93. void inicia(node *FILA)
  94. {
  95.  FILA->prox = NULL;
  96.  tam=0;
  97. }
  98.  
  99. int vazia(node *FILA)
  100. {
  101.  if(FILA->prox == NULL)
  102.   return 1;
  103.  else
  104.   return 0;
  105. }
  106.  
  107. node *aloca()
  108. {
  109.  node *novo=(node *) malloc(sizeof(node));
  110.  if(!novo){
  111.   printf("Sem memoria disponivel!\n");
  112.   exit(1);
  113.  }else{
  114.   printf("Novo elemento: "); scanf("%d", &novo->num);
  115.   return novo;
  116.  }
  117. }
  118.  
  119. void insere(node *FILA)
  120. {
  121.  node *novo=aloca();
  122.  novo->prox = NULL;
  123.  
  124.  if(vazia(FILA))
  125.   FILA->prox=novo;
  126.  else{
  127.   node *tmp = FILA->prox;
  128.  
  129.   while(tmp->prox != NULL)
  130.    tmp = tmp->prox;
  131.  
  132.   tmp->prox = novo;
  133.  }
  134.  tam++;
  135. }
  136.  
  137.  
  138. node *retira(node *FILA)
  139. {
  140.  if(FILA->prox == NULL){
  141.   printf("Fila ja esta vazia\n");
  142.   return NULL;
  143.  }else{
  144.   node *tmp = FILA->prox;
  145.   FILA->prox = tmp->prox;
  146.   tam--;
  147.   return tmp;
  148.  }
  149.  
  150. }
  151.  
  152.  
  153. void exibe(node *FILA)
  154. {
  155.  if(vazia(FILA)){
  156.   printf("Fila vazia!\n\n");
  157.   return ;
  158.  }
  159.  
  160.  node *tmp;
  161.  tmp = FILA->prox;
  162.  printf("Fila :");
  163.  while( tmp != NULL){
  164.   printf("%5d", tmp->num);
  165.   tmp = tmp->prox;
  166.  }
  167.  printf("\n        ");
  168.  int count;
  169.  for(count=0 ; count < tam ; count++)
  170.   printf("  ^  ");
  171.  printf("\nOrdem:");
  172.  for(count=0 ; count < tam ; count++)
  173.   printf("%5d", count+1);
  174.  
  175.  
  176.  printf("\n\n");
  177. }
  178.  
  179. void libera(node *FILA)
  180. {
  181.  if(!vazia(FILA)){
  182.   node *proxNode,
  183.      *atual;
  184.  
  185.   atual = FILA->prox;
  186.   while(atual != NULL){
  187.    proxNode = atual->prox;
  188.    free(atual);
  189.    atual = proxNode;
  190.   }
  191.  }
  192. }
Add Comment
Please, Sign In to add comment