2013-09-25 80 views
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();  
    } 
} 
+0

它是否必须是一个数组或是否可以使用其他集合? –

+0

你应该尝试编译第一个...这甚至不会编译。 'alpha [number] = new Node(word,first);'alpha [number]是一个LinkedList,而不是Node。这项任务是不可能的。编译器应该告诉你这一点。另外,你有一个'first'和一个'last'变量,但是你有26个链表。在add方法中,您将第一个和最后一个变量视为仅当前正在编辑的链接列表(alpha [index])的属性。这似乎有点可疑。如果是这种情况,你应该为每个链表设置一个'first'和一个'last'。 – Alderath

+0

你的代码有很少的设计问题。我的一个建议是,不要将节点类设为私有,使其成为公共的,并在'ArrayLinkedList'类中拥有一个Node的私有列表。 – codeMan

回答

0

你有这样做的原因吗? 我可以想出一个更简单的方法:使用Map将一个字符(例如“A”)映射到Words的LinkedList。

+0

不幸的是,我的作业概括了我使用一系列链表来按升序存储英文单词。而是以数字a = 0,b = 1,z = 25来引用字母a-z。 –