频率包 - getFrequencyOf(aData)

时间:2016-02-11 04:59:32

标签: java data-structures linked-list

遇到getFrequencyOf(aData)方法的麻烦,我在测试代码中搜索的每个元素都返回零...这是我的代码,如果有人能指出我做错了什么会非常感激< / p>

public class FrequencyBag<T>
{
    // TO DO: Instance Variables
    private Node firstNode;
    private int numberOfEntries;

    /**
     * Constructor
     * Constructs an empty frequency bag.
     */
    public FrequencyBag()
    {
        // TO DO
        firstNode = null;
        numberOfEntries = 0;
    }

    /**
     * Adds new entry into this frequency bag.
     * @param aData the data to be added into this frequency bag.
     */
    public void add(T aData)
    {
        // TO DO
        if(numberOfEntries != 0){
            Node newNode = new Node(aData);
            newNode.next = firstNode.next;
            firstNode = newNode;
            numberOfEntries++;
        }
        else{
            Node newNode = new Node(aData);
            firstNode = newNode;
        }

    }

    /**
     * Gets the number of occurrences of aData in this frequency bag.
     * @param aData the data to be checked for its number of occurrences.
     * @return the number of occurrences of aData in this frequency bag.
     */
    public int getFrequencyOf(T aData)
    {
        // TO DO
        int counter = 0;
        Node currentNode = firstNode; 
        for(int i = 1; i <= numberOfEntries; i++)
        {
            if(currentNode.data.equals(aData))
            {
                counter++;
                System.out.println(counter);
            }
            currentNode = currentNode.next;
        }
        System.out.println(counter);
        return counter;

    }

    /**
     * Gets the maximum number of occurrences in this frequency bag.
     * @return the maximum number of occurrences of an entry in this
     * frequency bag.
     */
    public int getMaxFrequency()
    {
        // TO DO
        Node currentNode = firstNode;
        int currentFrequency = 0;
        int maxFrequency = currentFrequency;
        for(int i = 1; i <= numberOfEntries; i++)
        {
            currentFrequency = getFrequencyOf(currentNode.data);
            if(currentFrequency > maxFrequency)
            {
                maxFrequency = currentFrequency;
            }
            currentNode = currentNode.next;
        }
        return maxFrequency;
    }

    /**
     * Gets the probability of aData
     * @param aData the specific data to get its probability.
     * @return the probability of aData
     */
    public double getProbabilityOf(T aData)
    {
        // TO DO
        int num = getFrequencyOf(aData);
        double probability = num / numberOfEntries;
        return probability;
    }

    /**
     * Empty this bag.
     */
    public void clear()
    {
        // TO DO
        firstNode.next = null;
        firstNode.data = null;
    }

    /**
     * Gets the number of entries in this bag.
     * @return the number of entries in this bag.
     */
    public int size()
    {
        // TO DO
        return numberOfEntries;
    }


    private class Node
    {
        private T data;
        private Node next;
        public Node (T a)
        {
            data = a;
            next = null;
        }
        public Node(T a, Node n)
        {
            data = a;
            next = n;
        }
    }


}

1 个答案:

答案 0 :(得分:1)

说实话,我没有阅读完整的代码,因为add方法有一个错误,这可能会导致你遇到的问题:

Node newNode = new Node(aData);
newNode.next = firstNode.next; //firstNode.next is null!
firstNode = newNode;

添加第一个节点时,它指向一个空节点(其下一个值为null)。

然后,当您添加第二个节点时,如上面一行所示,列表的新头指向前一个头的下一个节点,该节点始终为null。因此,您的列表始终只有一个节点firstNode。要解决此问题,请将以上行更改为:

Node newNode = new Node(aData);
newNode.next = firstNode;
firstNode = newNode;

OR(使用第二个构造函数):

Node newNode = new Node(aData,firstNode);
firstNode = newNode;

这将使您的新节点成为链接列表的头部,将下一个元素作为列表的前一个元素。

<强>更新 另外,另一个问题是numberOfEntries始终为0,因为在添加第一个节点时不会增加它。因此,在numberOfEntries++;方法的else块中添加add(),或者只移动块外的if块中存在的块bash