Chained lists in Java

Asked

Viewed 1,674 times

2

Assuming that I have a list chained Students, where one must register name and note, however this same student may contain more than one note. In my logic, he only registers a student with a single grade, as can be seen in the code below:

Class responsible for the Getters and Data Setters (the given "number" would be the identified of each student)

public class Lista {

    private String nome;
    private Lista prox;
    private int numero;
    private double nota;

    public double getNota() {
        return nota;
    }

    public void setNota(double nota) {
        this.nota = nota;
    }

    public int getNumero() {
        return numero;
    }

    public void setNumero(int numero) {
        this.numero = numero;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Lista getProx() {
        return prox;
    }

    public void setProx(Lista prox) {
        this.prox = prox;
    }

}

Class responsible for adding methods and later other functions

public class Aluno {

    Lista topo;
    Lista ultimo;
    int numero = 1;

    public String pilharAluno(String nome, double nota) {
        Lista novo = new Lista();
        novo.setNome(nome);
        novo.setNumero(this.numero);
        novo.setNota(nota);
        this.numero++;
        if (topo == null) {
            topo = novo;
            ultimo = novo;
            novo.setProx(null);
        } else {
            novo.setProx(topo);
            topo = novo;
        }
        return "Aluno cadastrado";
    }

}
  • Already tried to connect a list of grades in each student?

  • @Patrick I thought of this logic, but I can’t implement it.

  • 1

    I suggest you use C to work with data structures, because theoretically they are not object oriented but memory addresses!

  • @Danlucioprada, you need to implement the chained list, is that it? You can’t use LinkedList java? It wasn’t clear to me whether you just need to solve the problem you described or need to, in addition, implement the chained list.

  • @Dherik needs to implement a chained list for student registration, the problem is in the fact that each student can contain more than one grade. (no native Java functions, all in the form of manually written data structures)

  • 1

    @Danlucioprate you will need to create a new class with exactly the same functions as the List class but with different attributes. I recommend using inheritance in this case to not repeat code. If this comment does not help you understand, answer me that tomorrow I will draft a complete reply for you.

Show 1 more comment

1 answer

1


Use common sense, the student can(and should) have X grades. I think it’s a data structure job or something, make a generic list, and then specialize it for what you want.

    public class Lista<T>{
        Class<T> obj;
        Lista<T> prox;
        int tamanho = 0;
        ...
        {Métodos que qualquer lista usaria: insere, retira, tamanho etc...}
        ...
    }

This is an outline of a list with template on java, this T is a generic type that is defined at runtime, from that it is only do inheritance to modify the list object at your will.

In your case you would have a list of students that would be a legacy of List ex:

    public class ListaDeAlunos extends Lista<Aluno>{
        {Métodos que apenas uma lista de alunos possui(?)}
    }

If there are no methods/functions specific to a list of students, simply instantiate a generic list for students

Lista<Aluno> alunos = new Lista<Alunos>();

The student would be an Object whatever you would like to "collect" and in it would contain another heritage of grade list (Double) that would possess the specific functions/methods for a list of notes:

    public class ListaDeNotas extends Lista<Double>{
        {Métodos que apenas uma lista de notas possui: média, maior, menor...}

    }

Browser other questions tagged

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