1
I’m trying to solve this college exercise that I’ve already implemented a lot of, but there are still three things missing. It’s about data structure with Java OO - sequential list.
Follow the code of the list class:
public class ListaSequencial {
private Contato [] contatos = new Contato[10];
private int n = 0;
boolean isVazia(){
return n == 0;
}
boolean isCheia(){
return n == contatos.length;
}
int tamanho(){
return n;
}
private void deslocaDireita(int pos){
for(int i = n; i > pos;i--){
contatos[i] = contatos[i - 1];
}
}
boolean inserir(Contato contato){
if(isCheia()){
return false;
}
contatos[n] = contato;
n++;
return true;
}
boolean inserir(Contato c, int pos){
if(isCheia()){
return false;
}
deslocaDireita(pos);
contatos[pos] = c;
n++;
return true;
}
private void deslocaEsquerda(int pos){
for(int i = pos; i < n - 1 ;i++){
contatos[i] = contatos[i+1];
}
}
boolean remover(int pos){
if(pos < 0 || pos >=n){
return false;
}
deslocaEsquerda(pos);
n--;
return true;
}
public String imprimir(){
String str = "";
if(isVazia()){
return "lista vazia";
}
else{
for(int i = 0; i < n; i++){
str += contatos[i].nome + " - " + contatos[i].telefone + "\n";
}
return str;
}
}
Contato buscar(int pos){
if(pos < 0 || pos >= n){
return null;
}
return contatos[pos];
}
int getPosicao(Contato contato){
for(int i = 0; i < n; i++){
if(contatos[i] == contato){
return i;
}
}
return -1;
}
}
What I don’t know and need for Exercise:
I need to create a method to concatenate 2 lists;
I need to create a method that once the list fills, create a larger container (vector) so it never fills (like a loop);
Finally, I need a method to remove an element from the list by passing instead of the index, the element itself. In the case, remove the first occurrence found.
Somebody give me a hand?
Hello. Welcome to Sopt. You’re a little confused. What don’t you know? The algorithm? Plus, you have a lot of questions in one. Do our [tour] and guide how to ask a good question. Feel free to edit the question by adding details.
– Pablo Almeida
Well I reformulated the question at the end to understand better, the algorithm I know but I do not know how to do the 3 methods I put in the end. ( O to concatenate, create a method for the list as soon as it fills create a larger vector and Remove an element from the list by passing the element itself. The language I already know several things but in my view I’m still beginner and I’m 'Learning Strength" Data structure.
– Romário Júnior