Java 单词计数频率-链表

Java 单词计数频率-链表,java,linked-list,nodes,singly-linked-list,Java,Linked List,Nodes,Singly Linked List,我试图将单词插入到链接列表中,因为我想计算单词在列表中出现的次数,然后按从最高频率到最低频率的顺序返回单词。然而,我不断得到一个断言错误。下面是我的insert、getCount和getWords方法。似乎insert和getCount给我带来了问题 public class Frequency<E extends Comparable<E>> implements Iterable<E>{ private Node first; //starting

我试图将单词插入到链接列表中,因为我想计算单词在列表中出现的次数,然后按从最高频率到最低频率的顺序返回单词。然而,我不断得到一个断言错误。下面是我的
insert
getCount
getWords
方法。似乎
insert
getCount
给我带来了问题

public class Frequency<E extends Comparable<E>> implements Iterable<E>{
    private Node first; //starting node
    private Node parent;    //parent of currently processed node
    private int N;  //number of words


    /**
     * Linked List Node
     */
    private class Node{
        private E key;
        private int count;
        private Node next;

        Node(E e){
           key = e;
           count = 1;
           next = null;
        }

        Node(E e, Node r){
            key = e;
            count = 1;
            next = r;
         }

        @Override 
        public String toString(){
            return "("+key +","+count+")";
        }
    }

   /**
    * Inserts a word into linked list
    * @param key to be inserted 
    * @return true if the key is inserted successfully.
    */
    public boolean insert(E key){
        if (first == null || first.key != key) {
            first = new Node(key, first);
        } else {
            Node curr = first;
            while (curr.next != null) {
                curr.next = new Node(key, first.next);
            }
            curr = curr.next;
            N++;
        }
        return true;
    }

/**
     * 
     * @param key is the key to be searched for
     * @return frequency of the key. Returns -1 if key does not exist
     * 
     */
    public int getCount(E key){
        // go through the linked list and count the number of times each word appears
        // return the count of the word that is being called.
        if (key == null) {
            return -1;
        }
        int N = 0;
        Node curr = first;
        while (curr != null) {
            if (curr.key.equals(key)) {
                N++;
            }
            curr = curr.next;
        }
        return N;   
    }
    /**
     * Returns the first n words and count
     * @param n number of words to be returned
     * @return first n words in (word, count) format
     */
    public String getWords(int n){
        Node curr = first;
        for (int i = 1; i < n; i++) {
            curr = curr.next;
        }
        return curr.toString();
    }


    /**
     * Frequency List iterator
     */
    @Override
    public Iterator<E> iterator() {
        return new FreqIterator();
    }
    /**
     * 
     * Frequency List iterator class
     *
     */
    private class FreqIterator implements Iterator<E>{

        @Override
        public boolean hasNext() {
            Node curr = first;
            if(curr != null) {
                return true;
            }
            return false;
        }

        @Override
        public E next() {
            Node curr = first;
            if(hasNext() == false) {
                return null;
            }
            E item = curr.key;
            curr = curr.next;
            return item;
        }
    }
}


它不起作用,因为在
getCount(E键)
中,在迭代过程中从未检查传入的
键是否等于curr节点

对方法进行以下细微更改:

public int getCount(E key) {
    if (key == null) {
        return -1;
    }
    int N = 0;
    Node curr = first;
    while (curr != null) {
        if (curr.key.equals(key)) {  // change made here 
            N++;
        }
        curr = curr.next;
    }
    return N;
}

根据编辑,插入(E键)
中存在逻辑错误。您需要迭代以查找列表中的最后一个元素,然后将下一个引用分配给新创建的节点

public boolean insert(E key) {
    if (first == null || first.key != key) {
        first = new Node(key, first);
    } else {
        Node curr = first;
        while (curr.next != null) {  // iterate till the end of the list
            curr = curr.next; 
        }
        curr.next = new Node(key);   // point last node's next ref to new node
        N++;
    }
    return true;
}

请阅读:。谢谢!!当我尝试两次将同一个单词插入到链接列表中时,也会出现断言错误。例如,我想插入单词“dog”两次,所以我希望单词频率为2,但结果是1。不确定为什么我的
insert
没有插入两次单词。您仍然有问题吗?还是问题已经解决了?
getCount
工作正常,但我仍然有问题插入同一个单词两次。我刚刚编辑了我的帖子,包括我正在使用的测试,也解决了这个问题,看看编辑后的答案!
public boolean insert(E key) {
    if (first == null || first.key != key) {
        first = new Node(key, first);
    } else {
        Node curr = first;
        while (curr.next != null) {  // iterate till the end of the list
            curr = curr.next; 
        }
        curr.next = new Node(key);   // point last node's next ref to new node
        N++;
    }
    return true;
}