你好我试图在java中实现一个链表。 由于这是一项家庭作业,我不允许使用java中的内置LinkedList。
目前我已经实现了我的Node类
public class WordNode
{
private String word;
private int freq;
private WordNode next;
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word, WordNode next )
{
this.word = word;
this.next = next;
freq = 1;
}
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word)
{
this(word, null);
}
/**
*
*/
public String getWord()
{
return word;
}
/**
*
*/
public int getFreq(String word)
{
return freq;
}
/**
*
*/
public WordNode getNext()
{
return next;
}
/**
*
*/
public void setNext(WordNode n)
{
next = n;
}
/**
*
*/
public void increment()
{
freq++;
}
}
和我的“LinkedList”
public class Dictionary
{
private WordNode Link;
private int size;
/**
* Constructor for objects of class Dictionary
*/
public Dictionary(String L)
{
Link = new WordNode(L);
size = 1;
}
/**
* Return true if the list is empty, otherwise false
*
*
* @return
*/
public boolean isEmpty()
{
return Link == null;
}
/**
* Return the length of the list
*
*
* @return
*/
public int getSize()
{
return size;
}
/**
* Add a word to the list if it isn't already present. Otherwise
* increase the frequency of the occurrence of the word.
* @param word The word to add to the dictionary.
*/
public void add(String word)
{
Link.setNext(new WordNode(word, Link.getNext()) );
size++;
}
我无法正确实现我的添加方法,因为它必须检查列表,因为这个词已经存在,如果不存在,则将其添加到列表中并按字母顺序存储。 我一直在做一个while循环,但似乎无法让它工作。
编辑:我一直在尝试打印列表,但它不会打印超过第一个添加的单词 public void print() {
WordNode wn = Link;
int size = 0;
while(wn != null && size <= getSize()){
System.out.println(wn.getWord()+","+wn.getFreq(wn.getWord()));
size++;
}
}
感谢任何帮助
答案 0 :(得分:2)
您的添加方法错误。您正在获取根节点并将其下一个值设置为新节点。所以你永远不会有超过2个节点。如果你有0,它可能会因空指针而崩溃。
您要做的是将当前值设置为根节点,然后继续获取下一个节点,直到该节点为空。然后设置节点。
WordNode current = Link;
// Check if there's no root node
if (current == null) {
Link = new WordNode(word);
} else {
// Now that the edge case is gone, move to the rest of the list
while (current.getNext() != null) {
/* Additional checking of the current node would go here... */
current = current.getNext();
}
// At the last element, at then new word to the end of this node
current.setNext(new WordNode(word));
}
您需要保留上一个节点的实例,以便设置下一个值。如果没有节点开始会导致问题,那么需要一些额外的逻辑来不同地处理根节点。如果你永远不会有0个节点,那么你可以删除那个部分。
如果您还需要检查变量的值以查看它是否存在,您可以在while循环中添加一些内容,查看当前值并查看它是否等于您正在查找的当前单词对
答案 1 :(得分:1)
WordNode current = Link;
while (current != null) {
//check stuff on current word
current = current.getNext();
}
答案 2 :(得分:1)
如果没有您的循环代码,很难帮助您解决特定问题。但是,只是为了给你一点提示,根据说明你实际上不必搜索每个节点来找到这个词。这将使您有机会优化您的代码,因为只要您按字母顺序点击当前单词之后的单词,就可以停止查找并在当前单词之前立即将单词添加到列表中。
例如,如果您添加的单词是“badger”,而您的单词列表是
apple-->avocado-->beehive-->...
你知道蜜蜂应该在獾之后来,所以你不必继续寻找。您需要实现的是一种可以逐字母进行字母比较的方法,但我会留给您; - )
答案 3 :(得分:1)
假设您总是按字母顺序插入,它应该如下所示:
public void add(String word){
WordNode wn = Link;
WordNode prevNode = null;
while(wn != null){
if(word.equals(wn.getWord())){
//match found, increment frequency and finish
wn.increment();
return;
}else if(word.compareTo(wn.getWord) < 0){
//the word to add should come before the
//word in the current WordNode.
//Create new link for the new word,
//with current WordNode set as the next link
WordNode newNode = new WordNode(word, wn)
//Fix next link of the previous node to point
//to the new node
if(prevNode != null){
prevNode.setNext(newNode);
}else{
Link = newNode;
}
//increase list size and we are finished
size++;
return;
}else{
//try next node
prevNode = wn;
wn = wn.getNext();
}
}
//If we got here it means that the new word
//should be added to the end of the list.
WordNode newNode = new WordNode(word);
if(prevNode == null){
//if list was originally empty
Link = newNode;
}else{
//else append it to the end of existing list
prevNode.setNext(newNode);
}
//increment size and finish
size++;
}
答案 4 :(得分:0)
使用此代码:
class NodeClass {
static Node head;
static class Node {
int data;
Node next;
//constractor
Node(int d) {
data=d;
next=null;
}
}
static void printList(Node node ) {
System.out.println("Printing list ");
while(node!=null){
System.out.println(node.data);
node=node.next;
}
}
static void push(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
static void addInLast(int data) {
Node node = new Node(data);
Node n = head;
while(n.next!=null){
n= n.next;
}
n.next = node;
node.next = null;
}
public static void main (String[] args) {
NodeClass linklistShow = new NodeClass();
linklistShow.head = new Node(1);
Node f2 = new Node(2);
Node f3 = new Node(3);
linklistShow.head.next = f2;
f2.next =f3;
printList(linklistShow.head);
push(6);
addInLast(11);
printList(linklistShow.head);
}
}