Lista de acceso aleatorio
Las LAA están implementadas mediante un arreglo, en donde se aprovecha la rapidez de los arreglos para el acceso a cualquiera de sus elementos.
Implementaciones de estetipo son adecuados cuando el problema no demanda un número elevado de inserciones y/o eliminaciones.
Public interface InterfaceLista
{ void add(int index, object x);
void remove(intindex);
int indexOf(object x);
void clear();
boolean isEmpty();
int Length();
}
Public class ListaAleatoria implements InterfaceLista
{ private object Lista();private int size;
public ListaAleatoria(int capacidad)
{ lista = new object(capacidad);
size = 0;
}
//IMPLEMENTACION DE LOS OPERADORES (métodos)
Public voidadd(int index, object x)
{ if (index < 0 || index > size)
{ throw new IlegalArgumentsException
(“add: index < 0 || index > size “); }
If(isEmpty())lista[0]= x; // lista vacia
Else if (index ==0) // insertar al comienzo
{ moveR(index);
Lista(index) = x;
}
Else
If (index == size)// inserter al final
lista(index) = x;
else
{ moveR(index);
Lista(index) = x;
}Size++;
}
Public void remove (int index)
{ if (index < 0 || index > (size-1))
{ throw new IlegalArgumentsException(“add: index < 0 || index > size “);
}
If (index == 0) // Elimina al comienzo
moveL(index)
else
If( index != (size – 1) ) // Elimina al medio
moveL(index)
size --;
}}
Public int indexOf( object x)
{ int i;
for( i = x; i < size ; i++)
{ if(x.toString() == Lista[i].toString())
Break;
}
Return i==size ? -1:1; //si i=size retorna -1...
Regístrate para leer el documento completo.