Sequential list implementation exercise

Asked

Viewed 1,630 times

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:

  1. I need to create a method to concatenate 2 lists;

  2. I need to create a method that once the list fills, create a larger container (vector) so it never fills (like a loop);

  3. 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.

  • 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.

1 answer

2

(3) This is the easiest one. Do the following:

  • Create the new method to delete the element, the return is boolean.

  • Scroll through the array using a for. The elements to be covered are from 0 to < n.

  • Check inside your is if affixing the corresponding array has the element you are looking for. If you have, use the remover(int) to remove this position and return true right away.

  • To decide if the element found is what you are looking for or not, I recommend using the method java.util.Objects.equals(Object a, Object b).

  • If you have finished going through the array and do not find the element, return false.

(2) Growing the list is the most complicated of the three. Do the following:

  • Do the method isCheia() be private. The reason for this is that from the external point of view of the class, the list never gets full, since when it fills up, it will increase and no longer remain full, and therefore this method would no longer make sense to those who are looking at the outside class.

  • Create a private method crescer(), which is responsible for making the internal array grow in size. For this you create an array larger than the old one, copy all the elements and exchange the reference (contatos = novoArray;).

  • Instead of if (isCheia()) { return false; }, do if (isCheia()) { crescer(); }.

  • The methods inserir return void.

(1) To create the method that concatenates two lists do this:

  • This item is much easier if you have done part 2 first. Before implementing item 1, do item 2.

  • Create a static method that takes the two lists as a parameter and returns a list.

  • This method creates a new list. Scrolls through the lista1.n elements from the first list and add them to the new list. Then it traverses the lista2.n elements of the second list and does the same thing.

  • Return the new created list.

  • Buddy, I wouldn’t be able to post in code ?

  • 2

    @Yes, I would have. But since this is a college exercise for you to learn, I believe the idea is that you can write the code yourself to ensure learning.

  • The idea is this, but as it is a Virtual Academy I only have a few more minutes to post the rest of the Exercise and the Activity close, I tried to do something but I still have difficulty . It would really help if you posted .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.