有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java对象图作为列表的哈希

我有一个大学课时,我被困在一个部分

我必须用矩阵和列表的散列来表示图。第一个没问题。第二件事让我崩溃

我需要按从次要原点到较大原点的顺序打印图形的顺序,其中打印目的地和重量,如下所示:

出发地、目的地、重量

  • 0,1,1
  • 0,2,1
  • 1,3,2
  • 1,4,2
  • 2,3,2
  • 3,4,1

例如,如果打印了0,1,1,则无法打印1,0,1

但我的方法显示了所有这些,包括重复。我试图检查元素是否已经打印,但我无法调用从LIST类检查该元素的哈希函数

在我看来,层次结构是图形->;哈希->;以callin的形式列出

所以基本上我有列表的散列,散列索引是源,列表的元素是目标,有边

列表中单元格元素的代码如下所述:

    class Celula {
    public int elemento; 
    public int peso;
    public Celula prox; 
    public Celula ant; 

    Celula(int elemento, int peso) {
        this.elemento = elemento;
        this.peso = peso;
        this.prox = null;
        this.ant = null;
    }

    Celula(int elemento, int peso, Celula prox) {
        this.elemento = elemento;
        this.peso = peso;
        this.prox = prox;
        this.ant = null;
    }
 }

列表的代码如下所示:

public class Lista {
private Celula primeiro; 
private Celula ultimo; 

public Lista() {
    primeiro = new Celula(-1, -1);
    ultimo = primeiro;
}

public void mostrar(int a) {
    for (Celula i = primeiro.prox; i != null; i = i.prox) {
        if (i.elemento < a) {
            if(this.tabela[a].pesquisar(i) == false){
                System.out.println(a+","+i.elemento +","+i.peso);       
            }
        }
    }
  }

哈希的代码如下所示:

class HashIndiretoLista {
Lista tabela[];
int tamanho;
final int NULO = -1;

public HashIndiretoLista (){
    this(7);
}

public void mostrar(int size){
    for (int i = 0; i < size; i++) {
        tabela[i].mostrar(i);
    }
}

图的代码如下所示:

class Grafo{
int completo, tipo, tamanho;

public Grafo(){
    this.completo = 0;
    this.tipo = -1;
    this.tamanho = 0;
}

public Grafo(int tipo){
    this.tipo = tipo;
    this.completo = 0;
    this.tamanho = 0;
}

public static void build_graph(){
    String line;
    int ori, dest, weight, tipo, t_arestas, vertice;
    Arq.openRead("./pub.in");
    tipo = Integer.parseInt(Arq.readLine());
    t_arestas = Integer.parseInt(Arq.readLine());
    Grafo graph_info = new Grafo(tipo);
    graph.mostrar(t_arestas);
}

public static void main(String[] args) {
    build_graph();
 }
}

输入文件如下:
0
5
0,1,1
0,3,6
0,4,2
3,4,3
职能指令手册
3
2,4

列表中打印哈希列表的函数是public void mostrar(int a)


共 (1) 个答案

  1. # 1 楼答案

    我能弄明白。将方法更改为:

    public void mostrar(int a) {
        for (Celula i = primeiro.prox; i != null; i = i.prox) {
            if (i.elemento > a) {
                if(this.pesquisar(a) == false){
                    System.out.println(a+","+i.elemento +","+i.peso);       
                }
            }
        }
    }
    

    这使我能够使用散列表的函数pesquisar来调用方法mostrar。所以基本上是一个层次结构问题,我无法调用该方法进行验证,而没有这个。我在想,既然我在列表类中,这将调用列表类和它的方法。感谢所有对帮助感兴趣的人