1 / 30

Filas

Filas. #define MAXFILA 100 typedef struct queue { int item [ MAXFILA]; int inic, fim; } fila; fila q;. Operações sobre uma fila. q.item. 4. 3. 2. 1. q.inic = 0 q.fim = -1. 0. (a). No momento ( a) a fila está vazia q.inic=o e q.fim = -1

beyla
Télécharger la présentation

Filas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Filas

  2. #define MAXFILA 100 typedef struct queue { int item [ MAXFILA]; int inic, fim; } fila; fila q;

  3. Operações sobre uma fila.

  4. q.item 4 3 2 1 q.inic = 0 q.fim = -1 0 (a)

  5. No momento (a) a fila está vazia • q.inic=o e q.fim = -1 • Quantidade de elementos = q.fim –q.inic+1 • Ou seja: -1-0+1=0

  6. q.item 4 3 q.fim = 2 2 1 0 q.inic = 0 (b)

  7. No momento (b) • No momento (b) houve a inclusão de 3 elementos - 10, 20 e 30 • Quantidade de elementos = q.fim –q.inic+1 • Ou seja: 2 – 0 + 1 = 3

  8. q.item 4 3 q.inic = q.fim =2 2 1 0 (c)

  9. No momento (c) houve a remoção dos elementos 10 e 20 • q.fim = q.inic • 30 é o primeiro e o último elemento • Quantidade de elementos = q.fim –q.inic+1 • Ou seja: 2 – 2 + 1 = 1

  10. q.item q.fim = 4 4 3 q.inic = 2 2 1 0 (d)

  11. No momento (d) existem 4 -2 + 1 =3 • Não é possível inserir outros elementos, pois o elemento 50 ocupa a última posição do vetor • Existem 2 posições livres • Para deslocar os elementos no vetor exige grande esforço computacional X=q.item[0]; For (k=0;k< q.fim; k++) q.item[k]=q.item[k+1]; q.Fim --;

  12. Fila circular

  13. q.item q.fim = 4 4 3 q.inic = 2 2 1 0 (a)

  14. q.item 4 3 q.inic = 2 2 1 0 q.fim = 0 (b)

  15. q.item q.inic = 4 4 3 2 1 0 q.fim = 0 (c)

  16. q.item q.inic = 4 4 3 2 1 q.fim = 1 0 (d)

  17. q.item 4 3 2 1 q.fim = 1 0 q.inic = 0 (e)

  18. Como verificar se a fila está vazia • q.inic é o índice do vetor que é exatamente anterior ao primeiro elemento da fila • q.fim é o índice do último elemento da fila • Se q.inic=q.fim, temos uma fila vazia

  19. Ilustração da operação da inserção na fila

  20. q.item q.fim = 4 4 3 q.inic = 1 2 1 0 (a)

  21. q.item 4 3 2 1 q.inic = 1 0 q.fim = 0 (b)

  22. q.item 4 3 2 1 q.inic = q.fim = 1 0 (c)

  23. #define MAXFILA 100 typedef struct queue { int item [ MAXFILA]; int inic, fim; } fila ; Main () fila q;

  24. void inicFila (fila *pq) { pq→inic = MAXFILA -1; pq→fim = MAX FILA -1; }

  25. int filaVazia (fila *pq) { if(pq →inic==pq →fim) return (1) ; /*1 é verdadeiro* / else return (0) ; / *0 é falso*/ ) /* fim filaVazia*/

  26. if (filaVazia (&q)==1) /* tratamento adequado quando a fila está vazia */ else /* tratamento adequado quando a fila não está vazia */

  27. int remFila(fila*pq) { if(filaVazia (pq) ) { printf(“Underflow na fila!\n”); exit ( 1) ; } If (pq → inic = = MAXFILA -1 ) pq → inic = 0; else ( pq →inic) ++; return (pq → item [ pq → inic] ) ; } /*fim remove */

  28. void insFila ( fila *pq, int x) { /*realiza a movimentação para abrir espaço para novo elemento */ if (pq → fim = = MAXFILA – 1) pq → fim = 0 ; else (pq → fim ) ++ ; /*verifica ocorrência de estouro */ if (pq → fim = = pq → inic) { printf (“Ocorreu overflow na fila!\n”); exit ( 1 ) ; } pq → item [ pq → fim] = x; }

More Related