Skip to content

Instantly share code, notes, and snippets.

@hugofabricio
Created September 10, 2013 03:01
Show Gist options
  • Save hugofabricio/6504471 to your computer and use it in GitHub Desktop.
Save hugofabricio/6504471 to your computer and use it in GitHub Desktop.
public class ListaSequencial {
Carro [] carros = new Carro[100];
int n = 0;
boolean isVazia(){
return (n == 0);
}
boolean isCheia(){
return (n == carros.length);
}
int tamanho(){
return n;
}
Carro buscar(int pos) {
if ( pos >= n || pos < 0)
return null;
return carros[pos];
}
int getPosicao(Carro carro) {
for (int i=0; i < n; i++)
if (compara(carros[i], carro))
return i;
return -1;
}
boolean compara(Carro carro1, Carro carro2) {
return carro1.modelo.equals(carro2.modelo) && carro1.cor.equals(carro2.cor);
}
boolean inserir (int pos, Carro carro) {
if ( isCheia() || (pos > n) || (pos < 0) )
return false;
deslocaDireita(pos);
carros[pos] = carro;
n++;
return true;
}
void deslocaDireita(int pos) {
for (int i = n ; i > pos; i--)
carros[i] = carros[i -1];
}
boolean remover (int pos) {
if ( pos >= n || pos < 0)
return false;
deslocaEsquerda(pos); n--;
return true;
}
void deslocaEsquerda(int pos) {
for(int i=pos;i<n-1;i++)
carros[i] = carros[i+1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment