java - Java 中的哈希结构问题

标签 java linked-list stack hashtable

这是硬件问题:

此时,您决定为贡献者数据实现哈希结构以准备搜索。您将从提供的文件中读取贡献者信息;它是一个逗号分隔 (CSV) 文件。读取每条记录时,为 ID 字段创建一个哈希表。哈希表的限制是它的大小为 5,因此您需要能够处理冲突。应通过使用 ID 值的链接列表来解决冲突(使用堆栈来实现)。您的设计应包括以下内容:

指向链表结构的哈希表,仅包含以下信息:

每个哈希桶碰撞项目都会有以下信息:

ID:整数;// future 需要的标识符键 哈希桶函数/方法:

输入构造函数://接受每个贡献者的姓名和附加信息的字符串(您只需要输入数据的 ID 部分) 哈希函数构造函数:(提示:你只有5个哈希桶,所以该函数可以进行非常简单的计算。) 弹出构造函数 推送构造函数 打印构造函数://显示Hash桶的内容 可交付成果:

一个完整记录的程序,用于加载哈希表,并将冲突作为链接列表处理,并作为堆栈实现 显示程序如何运行和执行的测试计划 显示程序加载数据的截图,所有数据加载完毕后,显示第一个Hash桶的内容(理想情况下是Bucket 0)

我相信我已经非常接近了,但我已经习惯了 Python,所以 Java 语法是我正在努力学习的东西。不管怎样,请检查下面的代码看看我做了什么。

我认为问题与我声明哈希表大小的方式有关。在 python 中,我可以索引数组并将给定对象添加到数组中的该位置。看来我不能在java中做到这一点。我考虑尝试 for 循环,但没有成功

我觉得我已经很接近了。大部分代码是教授给出的,但我自己开发了 Stack() 类和方法。当我运行它们时,它们确实起作用了,我在这部分得到了 100% 的分数。

通过我所做的调试,我可以看到我正在初始化一个大小为“size”的数组(在本例中为 5)。但是,我不知道如何将键值对分配给给定的 Stack() 索引。

再说一遍,我习惯了Python,不太了解java,所以我真的认为是我不理解Java语法。

在 python 中,我只会按照 array[index].append[node] 的方式做一些事情。然后,我可以弹出数组[index]并一次显示一个节点。

import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;

public class ContributorManager {
    public static void main(String [] args) {
        Scanner inputFile = null;
        String name = null;
        String city = null;
        String country = null;
        String phone = null;
        double contribution = 0;
        int id = 0;
        Contributor c = null;
        Node node = null;
        HashTable h = new HashTable(5); 

        //open contributors file
        try {
            inputFile = new Scanner(new File("/Users/Dan/Desktop/contributors.csv"));
            System.out.println("AsdasdfaDSF");
            inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));
        }
        catch (Exception e) {
            System.err.println("Error opening file.");
        }

        //create contributors object for each row, and add to the stack
        while (inputFile.hasNext()) {
            name = inputFile.next();
            city = inputFile.next();
            country = inputFile.next();
            phone = inputFile.next();
            contribution = inputFile.nextDouble();
            id = inputFile.nextInt();
            inputFile.nextLine(); //advance to the next line

            c = new Contributor(name, city, country, phone, contribution, id); 
            node = new Node(c);
            //System.out.println(c.hashFunction());
            h.insert(node); //insert node into the hash table
        }

        h.print(); //print the entire hash table
    }
}
<小时/>
public class Contributor {
    private String name;
    private String city;
    private String country;
    private String phone;
    private double contribution;
    private int id;

    public Contributor(String name, String city, String country, String phone, double contribution, int id) {
        this.name = name;
        this.city = city;
        this.country = country;
        this.phone = phone;
        this.contribution = contribution;
        this.id = id;
    }

    public int hashFunction() {
        //calculate the hash key value using the id member variable in this object
        //the key must always return a value between 0 and 4
        int key = this.id % 5;
                return key;
        //return the hash key value
    }

    public void printContributor() {
        System.out.println("Name: " + name);
        System.out.println("City: " + city);
        System.out.println("Country: " + country);
        System.out.println("Phone: " + phone);
        System.out.println("Contribution: " + contribution);
        System.out.println("ID: " + id);
        System.out.println();
    }
}
<小时/>
import java.util.LinkedList;

public class HashTable {
    Stack[] table;
private int size;
    private int top;
        //declaring array

    public HashTable(int size) {
        //initialize the table array with empty Stack objects
        table = new Stack[size];
        System.out.println(table.length);



    }

    public void insert(Node n) {
        //determine the hash key of Node n
                System.out.println(n);
                int key = n.c.hashFunction();
                System.out.println(key);
        //using the key to determine the table location, 
        //push Node n onto the stack
                System.out.println(table.length);

                table[key].push(n);



    }

    public void print() {
        //display the contents of the entire table in order
        for (int i=0; i < table.length; i++) {
            System.out.println("===== Position " + i + " ======\n");
            table[i].print();
            System.out.println("========= End ==========");
            System.out.println();
        }

    }
}
<小时/>
public class Node {
    Contributor c;
    Node next;

    public Node(Contributor data){
        //initialize member variables
        c=data;
        next=null;
    }

   public void displayNode() {
    //display the contents of this node
        c.printContributor();
  }
}
<小时/>
public class Stack {
    Node first; 

    public Stack(){
        //initialize the empty stack
        first = null;
    }

    public void push(Node newNode){
        //if the stack is empty, make first point to new Node.
        if(first==null)
            first=newNode;  
        //if the stack is not empty, loop until we get to the end of the list,
        //then make the last Node point to new Node
        else
        {
            first=newNode;
            newNode = newNode.next;
         }
    }

    public Node pop() { 
        //if the stack is empty, return null
        if(first==null)
            return null;
        //Handle the case where there is only one Node in the stack  
        else if(first.next==null)
        {
            Node t=first;
            return t;   
        }
        //Handle the case where there are at Least two (or more) elements in the stack
        else
        {
            Node t=first;
            return t;   
        }  
    }

    public void print() {
        //display the entire stack
        Node tempDisplay = first; // start at the beginning of linkedList
        while (tempDisplay != null){ // Executes until we don't find end of list.
            tempDisplay.displayNode();
            tempDisplay = tempDisplay.next;
        }  
        System.out.println();
    }
}
<小时/>

贡献者.csv

Tim,Murphy,USA,8285557865,200,25
Gordon,Miner,USA,8285551008,150,32
Jean,Bell,USA,8285557503,225,33
Mike,Prather,USA,8285558497,155,34
George ,Pipps,USA,8285557777,100,35

最佳答案

However, I can't figure out how to assign a key value pair to a given Stack() index.

堆栈不是数组。索引分配不是一种受支持的操作,只能从结构的一端压入和弹出。

<小时/>

您似乎在推送和弹出操作方面遇到了问题?您的代码注释表明需要做什么。 如果堆栈不为空,则循环到末尾

您没有循环,并且您似乎向后插入堆栈。

相反,您可能需要类似以下的内容,与 print 方法非常相似

Node n = first;
while (n.next != null) n = n.next;
n.next = newNode;

与弹出类似,您可以在 n.next != null && n.next.next == null 时停止循环,以便您可以设置 n.next = null 并将其从列表中弹出

//TODO: loop logic 
Node toPop = n.next;
n.next = null;
return toPop;

关于java - Java 中的哈希结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58003354/

相关文章:

javascript - 堆叠 div 并使用切换来显示/隐藏

java - 如何在 Spring Boot RestController 中映射动态查询参数

java - 在这种情况下,我的 Switch 语句应该以 break 或 return x 退出吗?

c - 从 C 中的链表中弹出一个元素

c - C中的字符链表

c++ - 阿桑 : heap-use-after-free when flattening a binary tree to a linked list

java - fillRect 比 fillOval 快吗?

java - 如何从 eclipse url 中删除项目名称?

c - 超出时间限制 - 斐波那契

javascript - JavaScript 中的非重复堆栈