嘿伙计们,我正在尝试通过将前 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