Java 链表故障
我正在尝试创建一个链接的最后一个类,该类将存储数据以及数据出现的频率 例如,如果有人将整数5添加到链表中12次,当使用getFrequencyOf5询问频率5时,结果应该是12 不幸的是,添加到我的链接列表中的第一个数字返回的频率比它应该返回的频率低。我的代码贴在下面。如果您有任何关于如何解决此问题的想法,我们将不胜感激Java 链表故障,java,linked-list,Java,Linked List,我正在尝试创建一个链接的最后一个类,该类将存储数据以及数据出现的频率 例如,如果有人将整数5添加到链表中12次,当使用getFrequencyOf5询问频率5时,结果应该是12 不幸的是,添加到我的链接列表中的第一个数字返回的频率比它应该返回的频率低。我的代码贴在下面。如果您有任何关于如何解决此问题的想法,我们将不胜感激 package main; // //Implement using a linked list public class FrequencyBag<T>
package main;
// //Implement using a linked list
public class FrequencyBag<T>
{
// TO DO: Instance Variables
private Node firstNode;
private int numOfItems;
private class Node{
//Node Instance Variables
private T data;
private int frequency;
private Node next;
private Node(T Data, Node nextNode){
data= Data;
next=nextNode;
frequency=1;
}
private void addF(){
frequency++;
}
private int getF(){
return frequency;
}
}
/**
* Constructor
* Constructs an empty frequency bag.
*/
public FrequencyBag()
{
firstNode=null;
numOfItems =0;
}
/**
* Adds new entry into this frequency bag.
* @param aData the data to be added into this frequency bag.
*/
public void add(T aData)
{
if(firstNode!=null){
boolean found =false;
Node currNode=firstNode;
while(currNode.next !=null){
if(currNode.data.equals(aData)){
currNode.addF();
found=true;
break;
}
currNode= currNode.next;
}
if(!found){
Node tempNode=firstNode;
firstNode= new Node(aData,tempNode);
}
}
else{
firstNode= new Node(aData,null);
}
numOfItems++;
}
/**
* 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)
{
Node currNode= firstNode;
while(currNode!=null){
if(currNode.data.equals(aData)){
return currNode.getF();
}
currNode= currNode.next;
}
return 0;
}
/**
* 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()
{
if(firstNode!=null){
Node currNode= firstNode;
Node maxNode= firstNode;
while(currNode!=null){
if(currNode.getF()>=maxNode.getF()){
maxNode=currNode;
}
currNode=currNode.next;
}
return maxNode.getF();
}
else{
return 0;
}
}
/**
* Gets the probability of aData
* @param aData the specific data to get its probability.
* @return the probability of aData
*/
public double getProbabilityOf(T aData)
{
if(firstNode!=null){
boolean flag=true;
double dataF=0;
double prob=0;
Node currNode= firstNode;
while(currNode!=null){
if(currNode.data.equals(aData)){
dataF=(double)currNode.getF();
flag=false;
break;
}
currNode= currNode.next;
}
if(!flag){
prob= dataF/(double)numOfItems;
}
return prob;
}
else{
return 0;
}
}
/**
* Empty this bag.
*/
public void clear()
{
firstNode=null;
numOfItems=0;
}
/**
* Gets the number of entries in this bag.
* @return the number of entries in this bag.
*/
public int size()
{
return numOfItems;
}
}
你在add中的逻辑是错误的。如果只添加了一次,即在第二次添加操作中,因为firstNode.next在该点为null,则跳过第一个节点而不进行测试
创建您的while currNode.next!=一段时间后为空节点!=无效的请参阅下面的特定问题的正确版本:我没有测试所有预期行为,因为我没有您的规范
请注意,代码可以如图所示缩短,然后再进一步缩短。开始编写单元测试,您既可以更快地发现类似这样的错误,也可以更有信心地进行重构,以使代码更短、更易于理解
更正:
public void add(T aData)
{
if(firstNode!=null){
boolean found =false;
Node currNode=firstNode;
while(currNode != null){
if(currNode.data.equals(aData)){
currNode.addF();
found=true;
break;
}
currNode= currNode.next;
}
if(!found){
Node tempNode=firstNode;
firstNode= new Node(aData,tempNode);
}
}
else {
firstNode= new Node(aData,null);
}
numOfItems++;
}
更正和缩短:
public void add(T aData)
{
boolean found =false;
Node currNode=firstNode;
while(currNode != null){
if(currNode.data.equals(aData)){
currNode.addF();
found=true;
break;
}
currNode= currNode.next;
}
if(!found){
Node tempNode=firstNode;
firstNode= new Node(aData,tempNode);
}
numOfItems++;
}
您应该在调试器下运行代码,以查看出了什么问题。仅仅通过查看来调试它太长了。只需对频率使用哈希表就可以了——在绝大多数情况下更简单、更高效、更节省内存,这不是问题。@ILoveCoding。如果我能-1你的评论,我会的。他应该写出来,为什么它不起作用,使这个代码保持和正确。“乌龟我已经告诉你如何纠正这个代码,但是一旦你把它固定在你的代码中,你应该考虑把它放在以得到关于你的方法的一般反馈,因为我可以看到一些可以改进的东西。谢谢你,安迪!我会马上发布我的代码!