-
-
Save tonussi/5886339 to your computer and use it in GitHub Desktop.
Resolução questão Fila
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
public class Fila { | |
private final int[] dados; | |
private int inicio, fim, contador; | |
private final int max; | |
public boolean filaCheia() { | |
return contador == max; | |
} | |
public boolean filaVazia() { | |
return contador == 0; | |
} | |
public Fila(int tamanho) { | |
dados = new int[tamanho]; | |
max = tamanho; | |
inicio = 0; | |
fim = -1; | |
contador = 0; | |
} | |
// Questão 4 | |
public void entraNaFrente(int valor) { | |
if (filaCheia()) { | |
throw new RuntimeException("Fila Cheia"); | |
} else { | |
contador += 1; | |
if (inicio != 0) { | |
dados[inicio - 1] = valor; | |
inicio -= 1; | |
} else { | |
for (int i = fim; i >= 0; i--) { | |
dados[i + 1] = dados[i]; | |
inicio = 0; | |
} | |
dados[0] = valor; | |
} | |
fim += 1; | |
} | |
} | |
// entraNoFim | |
public void entrarNaFila(int valor) { | |
if (filaCheia()) { | |
throw new RuntimeException("Fila Cheia"); | |
} else { | |
fim = fim + 1; | |
if (fim > max) { | |
fim = 0; | |
dados[fim] = valor; | |
contador++; | |
} | |
} | |
} | |
// sai na frente | |
public int saiDaFila() { | |
if (filaVazia()) { | |
throw new RuntimeException("Fila Vazia"); | |
} else { | |
int ret = dados[inicio]; | |
inicio += 1; | |
if (inicio > max) { | |
inicio = 0; | |
contador--; | |
return ret; | |
} | |
return ret; | |
} | |
} | |
// Questão 4 | |
public int saiDeTras() { | |
if (fim != -1) { | |
int aux = fim; | |
fim -= 1; | |
contador -= 1; | |
return dados[aux]; | |
} else { | |
throw new RuntimeException("Fila Vazia"); | |
} | |
} | |
} |
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
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class FilaTest { | |
private Fila fila; | |
@Before | |
public void setUp() throws Exception { | |
fila = new Fila(6); | |
// Preenche a fila com ordenação crescente. | |
for (int valor = 6; valor >= 1; valor--) { | |
fila.entraNaFrente(valor); | |
} | |
} | |
@Test | |
public void deveSaberTirarDeTras() { | |
// Situacao atual da lista (1,2,3,4,5,6) | |
fila.saiDeTras(); // Deve retirar o 6 | |
int deveSer5 = fila.saiDeTras(); // Deve retirar o 5 | |
Assert.assertEquals(5, deveSer5, 0.0001); | |
} | |
@Test | |
public void deveSaberIncluirNaFrente() { | |
Fila filaTeste = new Fila(2); | |
filaTeste.entraNaFrente(200); | |
filaTeste.entraNaFrente(1234); | |
Assert.assertEquals(1234, filaTeste.saiDaFila()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment