Baixe o app para aproveitar ainda mais
Prévia do material em texto
Exemplo de implementação do tipo abstrato de dados FILA #include <stdio.h> #define TRUE 1 #define FALSE 0 typedef struct no { int info; struct no * prox; } TNo; typedef struct descritor { TNo *inicio, *fim; } TDescritor; typedef TDescritor * Queue; int isEmpty (Queue fila) { if (fila->inicio == NULL) return TRUE; else return FALSE; } void enqueue (Queue fila, int n) { TNo * novo; novo = (TNo *) malloc (sizeof (TNo)); novo->info = n; novo->prox = NULL; if (isEmpty (fila) == TRUE) fila->inicio = novo; else fila->fim->prox = novo; fila->fim = novo; } int dequeue (Queue fila) { TNo * aux; int n; aux = fila->inicio; fila->inicio = fila->inicio->prox; if (isEmpty (fila) == TRUE) fila->fim = NULL; n = aux->info; free (aux); return n; } int head (Queue fila) { return fila->inicio->info; } void initialize (Queue * fila) { *fila = (TDescritor *) malloc (sizeof (TDescritor)); (*fila)->inicio = NULL; (*fila)->fim = NULL; } � int main () { Queue minhaFila; int valor; char op; initialize (&minhaFila); do { printf ("1 - Enfileirar \n2 - Desenfileirar \n3 - Ver inicio \n4 - Sair \nDigite a opcao: "); op = getchar (); fflush (stdin); switch (op) { case '1': printf ("Informe o valor: "); scanf ("%i",&valor); fflush (stdin); enqueue (minhaFila, valor); break; case '2': if (isEmpty (minhaFila) == TRUE) printf ("Fila vazia \n"); else { valor = dequeue (minhaFila); printf ("Valor desenfileirado: %i\n", valor); } break; case '3': if (isEmpty (minhaFila) == TRUE) printf ("Fila vazia \n"); else printf ("Valor no inicio da fila: %i\n", head(minhaFila)); break; case '4': while (isEmpty (minhaFila) == FALSE) dequeue (minhaFila); break; default: printf ("Opcao invalida \n"); } } while (op != '4'); return 0; }
Compartilhar