Created
November 21, 2013 18:33
-
-
Save edenfall/7586986 to your computer and use it in GitHub Desktop.
FAFIT - Sistemas de Informação - 2º semestre 2013
Estruturas de Dados I - Professor Danilo
Exemplo de Fila Circular de 2013-11-14
2013-11-14-exemplo.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define T 5 | |
struct ST_Fila { | |
int | |
n, | |
ini; | |
float | |
vet[T]; | |
}; | |
typedef struct ST_Fila Fila; | |
void filaCria (Fila* f); | |
void filaInsere (Fila* f); | |
void filaRetira (Fila* f); | |
void filaExibe (Fila* f); | |
int filaVazia (Fila* f); | |
int main () { | |
int | |
opc; | |
Fila | |
fila; | |
filaCria(&fila); | |
do { | |
system("clear"); | |
printf( | |
"1 - Inserir elemento\n" | |
"2 - Retirar elemento\n" | |
"3 - Exibir fila\n" | |
"0 - Sair\n" | |
); | |
scanf("%d", &opc); | |
getchar(); | |
switch (opc) { | |
case 1: | |
filaInsere(&fila); | |
break; | |
case 2: | |
filaRetira(&fila); | |
break; | |
case 3: | |
filaExibe(&fila); | |
break; | |
case 0: | |
break; | |
default: | |
printf("\n\nOpção incorreta!"); | |
getchar(); | |
break; | |
} | |
} while (opc != 0); | |
} | |
void filaCria (Fila* f) { | |
f->n = 0; | |
f->ini = 0; | |
} | |
void filaInsere (Fila* f) { | |
int | |
fim; | |
if (f->n == T) { | |
printf("Fila cheia!"); | |
} else { | |
fim = (f->ini + f->n) % T; | |
printf("Informe um valor: "); | |
scanf("%f", &f->vet[fim]); | |
f->n++; | |
} | |
getchar(); | |
} | |
void filaRetira (Fila* f) { | |
if (filaVazia(f)) { | |
printf("Fila vazia!"); | |
} else { | |
printf("Elemento removido: %.2f", f->vet[f->ini]); | |
f->ini = (f->ini + 1) % T; | |
f->n--; | |
} | |
getchar(); | |
} | |
void filaExibe (Fila* f) { | |
int | |
a1, | |
pos, | |
fim; | |
if (filaVazia(f)) { | |
printf("Fila vazia!"); | |
} else { | |
for (a1 = 0, pos = f->ini, fim = (f->ini + f->n) % T; a1 < f->n; a1++, pos = (pos + 1) % T) { | |
printf("{%d: %.2f}\n", pos, f->vet[pos]); | |
} | |
} | |
getchar(); | |
} | |
int filaVazia (Fila* f) { | |
return f->n == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment