0
我的程序是爲了獲取單詞列表並將每個單詞按照升序存儲在數組中的字母引用下。例如A-Z單詞蘋果的數組,A下的一個鏈接列表下的A被0引用,Zebra下的Z被引用25.但是當我使用標準的first = new Node(單詞)時,我沒有添加任何內容。我絕望地迷失了。創建一個鏈接列表
import java.util.LinkedList;
public class ArrayLinkedList
{
/**
The Node class is used to implement the
linked list.
*/
private class Node
{
String value;
Node next;
/**
* Constructor
* @param val The element to store in the node
* @param n The reference to the successor node
*/
Node(String val, Node n)
{
value = val;
next = n;
}
Node(String val)
{
this(val, null);
}
}
private final int MAX = 26; // Number of nodes for letters
private Node first; // List head
private Node last; // Last element in the list
private LinkedList[] alpha; // Linked list of letter references
/**
* Constructor to construct empty array list
*/
public ArrayLinkedList()
{
first = null;
last = null;
alpha = new LinkedList[MAX];
for (int i = 0; i < MAX; i++)
{
alpha[i] = new LinkedList();
}
}
/**
* arrayIsEmpty method
* To check if a specified element is empty
*/
public boolean arrayIsEmpty(int index)
{
return (alpha[index].size() == 0);
}
/**
* The size method returns the length of the list
* @return The number of elements in the list
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count++;
p = p.next;
}
return count;
}
/**
* add method
* Adds the word to the first position in the linked list
*/
public void add(String e)
{
String word = e.toLowerCase(); // Put String to lowercase
char c = word.charAt(0); // to get first letter of string
int number = c - 'a'; // Index value of letter
// Find position of word and add it to list
if (arrayIsEmpty(number))
{
first = new Node(word);
first = last;
}
else
{
first = sort(first, word, number);
}
}
/**
* nodeSort method
* To sort lists
*/
private Node sort(Node node, String value, int number) {
if (node == null) // End of list
{
return getNode(value, number);
}
int comparison = node.value.compareTo(value);
if (comparison >= 0) // Or > 0 for stable sort.
{
Node newNode = getNode(value, number); // Insert in front.
newNode.next = node;
return newNode;
}
node.next = sort(node.next, value, number); // Insert in the rest.
return node;
}
private Node getNode(String value, int number)
{
return first.next;
}
/**
* get method
* to get each word value from the linked list and return it
* @return value
*/
public LinkedList get(int index)
{
return alpha[index];
}
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("Word and occurrence in ascending order\n\n");
Node p = first;
while (p != null)
{
sBuilder.append(p.value + "\n");
p = p.next;
}
return sBuilder.toString();
}
}
它是否必須是一個數組或是否可以使用其他集合? –
你應該嘗試編譯第一個...這甚至不會編譯。 'alpha [number] = new Node(word,first);'alpha [number]是一個LinkedList,而不是Node。這項任務是不可能的。編譯器應該告訴你這一點。另外,你有一個'first'和一個'last'變量,但是你有26個鏈表。在add方法中,您將第一個和最後一個變量視爲僅當前正在編輯的鏈接列表(alpha [index])的屬性。這似乎有點可疑。如果是這種情況,你應該爲每個鏈表設置一個'first'和一個'last'。 – Alderath
你的代碼有很少的設計問題。我的一個建議是,不要將節點類設爲私有,使其成爲公共的,並在'ArrayLinkedList'類中擁有一個Node的私有列表。 – codeMan