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

时间:2013-11-05 19:34:34

标签: java linked-list nodes

嘿伙计们我试图通过将前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 
}

fileIn class:

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

2 个答案:

答案 0 :(得分:2)

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

您获得的结果相同,因为指数始终为2。这导致fra15×262 + 27×262 + 10×262 = 35,152)和lal21×262 + 10×262 + 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--; 
}

或者您可以将j替换为2 - i并完全删除额外的变量。

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

答案 1 :(得分:0)

看起来你的数学错了。 Character.getNumericValue(a)不会像你想象的那样返回0到25之间的值。 如果你想根据第3个字母进行排序并使用它,只需制作一个自定义Comparator类。

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