按字母顺序对名称的链接列表进行排序



嘿伙计们,我正在尝试通过将前 3 个字母相乘来按字母顺序对链表进行排序。 它的工作方式是第一个字母需要 26^2,第二个字母是 26^1,第三个字母是 26^0。 当我运行该程序时,它给了我相同的金额,比如说"lala"和"francis"这个名字。 如果有人能帮助我看到代码有什么问题,将不胜感激!

LinkedListNode 类:(包含 getSum 方法)

public class LinkedListNode 
{ 
    public String data; 
    public LinkedListNode next; 
   public long sum; 

    public LinkedListNode(String data)  
   { 
        this.data = data; 
        this.next = null; 
        this.sum = getSum(data); 
    }//end node 
   public long getSum(String line) 
    { 
        int i; 
        long sum = 0; 
        String s = null; 
      char a; 
           for(i=0; i < 3; i++) 
         { 
            int j = 2; 
            a = line.charAt(i);      
            sum += Character.getNumericValue(a) * Math.pow(26, j); 
            //Return the value of the number 4 to be the power of 3 (4*4*4): Math.pow(4,3); 
            j--; 
           }//end for  
        return sum; 
    }//end getSum 
    public long getSum() 
    { 
      return sum; 
    }//end getSum 
    public String getData()  
   { 
        return data; 
    }//end getData 
    public void setData(String data)  
   { 
        this.data = data; 
    }//end setData 
    public LinkedListNode getNext()  
   { 
        return next; 
    }//end node 
    public void setNext(LinkedListNode next)  
   { 
        this.next = next; 
    }//end setNext 
}//end class node

LinkedList 类:(列表有其他方法)

public class LinkedList { 
    public LinkedListNode front; 
    public LinkedList() { 
        this.front = null; 
    } 
    public void insertBack(String data) 
   { 
        if(front == null){ 
            front = new LinkedListNode(data); 
        }else{ 
            LinkedListNode newNode = new LinkedListNode(data); 
            LinkedListNode current = front; 
            while(current.getNext() != null){ 
                current = current.getNext(); 
            } 
            current.setNext(newNode); 
        }        
    }//end insertBack 
   public void addAfter(LinkedListNode spot, String data) 
   { 
       LinkedListNode newNode; 
       newNode = new LinkedListNode(data); 
       newNode.next = spot.next; 
       spot.next = newNode; 
   }//end addAfter 
   public void addBefore(LinkedListNode spot, String data) 
   { 
   }//end addBefore    
   public void deleteAfter(LinkedListNode spot) 
   { 
       LinkedListNode nextNode; 
       nextNode = spot.next; 
       spot.next = nextNode.next; 
   }//end deleteAfter 

    public String showList() 
   { 
        int i = 0; 
        String retStr = "The nodes in the list are:n"; 
        LinkedListNode current = front; 
        while(current != null){ 
            i++; 
            retStr += "Node " + i + " is: " + current.getData() + " and the sum is: " + current.getSum() + "n"; 
            current = current.getNext(); 
        } 
        return retStr; 
    } 
   public LinkedListNode findTail() 
   { 
       LinkedListNode current = front; 
        while(current.getNext() != null) 
      { 
            current = current.getNext(); 
        } 
      return current; 
   }//end findTail 
}

文件在类中:

import java.util.Scanner; 
import java.io.*; 
public class fileIn 
{ 
   LinkedListNode front; 
   LinkedList myList = new LinkedList(); 
   String fname; 
   public static void main(String[] args) 
    {    
       fileIn f = new fileIn(); 
   }//end main 

   public fileIn() 
   { 
      getFileName(); 
      readFileContents(); 
      System.out.print(myList.showList()); 
   }//end namesLinkedList 
   public void readFileContents() 
    { 
        boolean looping; 
        DataInputStream in; 
        String line; 
        int j, len; 
        char ch; 
        /* Read input from file and process. */
        try 
        { 
            in = new DataInputStream(new FileInputStream(fname)); 
            looping = true; 
            while(looping) 
             { 
                /* Get a line of input from the file. */
                if (null == (line = in.readLine()))  
                { 
                    looping = false; 
                    /* Close and free up system resource. */
                    in.close(); 
                }//end if 
                else 
                { 
                myList.insertBack(line); 
                    j = 0; 
                    len = line.length();   
                }//end else 
            } /* End while. */
        } /* End try. */
        catch(IOException e)  
        { 
            System.out.println("Error " + e); 
        } /* End catch. */
    }//end readFileContents 
     public void getFileName() 
     { 
        Scanner in = new Scanner(System.in); 
        System.out.println("Enter file name please."); 
        fname = in.nextLine(); 
     }//end getFileName 
}//end class namesLinkedList
for (i = 0; i < 3; i++) { 
    int j = 2; 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, j); 
    j--; 
}

你会得到相同的结果,因为指数总是2。这导致 fra (15×262 + 27×26 2 + 10×26 2 = 35,152) 和 lal (21×262 + 10×26 2 + 21×262 = 35,152) 的值相同。这是为什么呢?

变量j在循环内部声明,而不是在循环外部声明。末尾的递减没有影响,因为它在每次迭代开始时从2开始。

您应该将声明移出循环:

int j = 2; 
for (i = 0; i < 3; i++) { 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, j); 
    j--; 
}

或者你可以用2 - i替换j,并完全摆脱额外的变量。

for (i = 0; i < 3; i++) { 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, 2 - i); 
}

看起来你的数学是错误的。Character.getNumericValue(a) 不会像您认为的那样返回 0 到 25 之间的值。如果您想根据 fist 3 个字母进行排序并使用它,只需创建一个自定义比较器类。

编辑:我对getNumericValue的工作方式是错误的,但数学仍然是错误的(见下面的评论)。

相关内容

  • 没有找到相关文章

最新更新