#include <stdio.h>
#define MAX 5 // numero maximo de elementos na fila
// cria uma fila vazia
int comeco = 0; // comeco da fila
int tamanho = 0; // tamanho da fila (numero de elementos)
int queue[MAX]; // vetor da fila
void inserir( int ); // inserir elementos no fim da fila
void remover( void ); // remover elementos do comeco da fila
int main(void)
{
int i; // contador
inserir(1);
inserir(10);
inserir(100);
inserir(1000);
remover();
inserir(6);
remover();
inserir(60);
//// mostra fila na tela ////
for(i = 0; i < MAX; i++)
printf("fila[%i] = %i\n", i
, queue
);
// system("pause"); // comente esta linha se for rodar no linux
return ( 0 );
} // fim main
void inserir( int elemento )
{
//// checa se a fila esta cheia ////
if( tamanho == MAX )
else {
//// converte os valores virtuais (tamanho e comeco) para o valor real utilizando o operador modulo ////
queue[ ((comeco + tamanho) % MAX) ] = elemento;
//// incrementa tamanho da fila (elemento foi inserido) ////
tamanho ++;
}
} // fim funcao
void remover(void)
{
//// checa se a fila esta vazia ////
if( tamanho == 0 )
else {
//// apaga o primeiro elemento da fila deslocando o ponteiro do comeco para proximo elemento ////
comeco ++;
//// decrementa o contador de tamanho (um valor foi removido) ////
tamanho --;
}
} // fim funcao
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE1BWCA1IC8vIG51bWVybyBtYXhpbW8gZGUgZWxlbWVudG9zIG5hIGZpbGEKCi8vIGNyaWEgdW1hIGZpbGEgdmF6aWEKaW50IGNvbWVjbyA9IDA7ICAgLy8gY29tZWNvIGRhIGZpbGEKaW50IHRhbWFuaG8gPSAwOyAgLy8gdGFtYW5obyBkYSBmaWxhIChudW1lcm8gZGUgZWxlbWVudG9zKQppbnQgcXVldWVbTUFYXTsgICAvLyB2ZXRvciBkYSBmaWxhCgp2b2lkIGluc2VyaXIoIGludCApOyAgICAvLyBpbnNlcmlyIGVsZW1lbnRvcyBubyBmaW0gZGEgZmlsYQp2b2lkIHJlbW92ZXIoIHZvaWQgKTsgICAvLyByZW1vdmVyIGVsZW1lbnRvcyBkbyBjb21lY28gZGEgZ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