2012-01-16 214 views
5

数据结构类,实现具有头部,尾部和当前节点的单个链接列表。遇到方法问题,可以使用正确方向的微调。Java链接列表 - 添加方法

从分配,编写方法:

加(项目)增加了在列表中的当前节点之后的项目(字符串),并将当前的指针指到新节点。

我尝试:

我add方法似乎只当我将项目添加到列表中,而不是在两端的工作。如果我用它来添加一些项目然后打印列表,那么只有我添加的第一个项目会在列表中,而我的prepend和append方法已经测试得很好。

我的代码有什么明显的问题吗?我觉得我失去了一些明显的东西。

所有:

public class LinkedList { 
    Node head = null; /* Head of the list */ 
    Node tail = null; /* Tail of the list */ 
    Node curr = null; /* Current node in the list */ 

    public void prepend(String item) { 
     if (head == null) { 
      head = tail = new Node(item, null); 
      curr = head; 
     } else { 
      head = new Node(item, head); 
      curr = head; 
     } 
    } 

    public void append(String item) { 
     if (head == null) { 
      head = tail = new Node(item, null); 
      curr = tail; 
     } else { 
      tail.next = new Node(item, null); 
      tail = tail.next; 
      curr = tail; 
     } 
    } 

    public void add(String item) { 
     if (curr != null) { 
      Node newNode = new Node(item, curr.next); 
      curr.next = newNode; 
      curr = newNode; 
     } else { 
      head = tail = new Node(item, null); 
      curr = head; 
     } 
    } 

    public void delete() { 
     if (curr.next == null) { 
      Node temp = head; 
      while (temp.next != curr) { 
       System.out.println(temp.item); 
       temp = temp.next; 
      } 
      temp.next = null; 
      curr = head; 
     } 
    } 

    public void find(String item) { 
     Node temp = new Node(curr.item, curr.next); 
     if (item.equals(temp.item)) 
      curr = temp; 
     else { 
      temp = temp.next; 
      while (temp.next != null && temp != curr) { 
       if (item.equals(temp.item)) 
        curr = temp; 
      } 
     } 
    } 

    public String get() { 
     if (curr != null) 
      return curr.item; 
     else 
      return ""; 
    } 

    public boolean next() { 
     if (curr != tail) { 
      curr = curr.next; 
      return true; 
     } else 
      return false; 
    } 

    public void start() { 
     curr = head; 
    } 

    public void end() { 
     curr = tail; 
    } 

    public boolean empty() { 
     if (head == null) 
      return true; 
     else 
      return false; 
    } 
} 

Node类:

class Node { 
    Node next; 
    String item; 

    Node(String item, Node next) { 
     this.next = next; 
     this.item = item; 
    } 
} 
+2

其他代码呢? – fge 2012-01-16 19:47:04

+0

该部分看起来不错,所以向我们展示周围的代码,错误必须在那里。 – 2012-01-16 19:57:33

+0

增加了代码的其余部分 – dysania 2012-01-16 19:57:47

回答

0

我认为这个问题是

if (curr != null) { 
    Node newNode = new Node(item, curr.next); //<-- here (curr.next) 

//and 

Node(String item, Node next) { 
    this.next = next; //<-- here 

尝试(编辑):

Node newNode = new Node(item, curr); // pass curr to the constructor of Node 
curr = newNode; 
+1

我认为这会使节点指向自己而不是下一个节点。 – vextorspace 2012-01-16 19:55:36

+1

不,这会断开连接列表,当前'curr'不会指向插入的节点。 – 2012-01-16 19:56:06

+1

如果你这样做,你会失去元素之间的联系...因为那么节点都将指向自己。我认为他的代码很好:您首先将当前变量的下一个值分配给新节点,然后让新节点成为当前变量。我感觉合理。 – Chnoch 2012-01-16 19:57:17

1

我在这里没有看到任何问题,所以我猜这个问题是在其他地方。

好吧,我看到有唯一的问题是在删除:

public void delete() 
{ 
    Node temp = head; 

    while(temp != null && temp.next != curr) { 
     System.out.println(temp.item); 
     temp=temp.next; 

    } 

    if (temp != null && temp.next != null) { 
     temp.next = temp.next.next; 
    } 
    curr = head; 

} 
+0

嗯,这是我第一次使用提供的驱动程序文件来测试我的代码,所以也许问题在那里..谢谢你看虽然。 – dysania 2012-01-16 20:06:10

+0

欣赏帮助,但我已经停止了删除工作,一旦我意识到add没有正确测试,还没有完成测试find方法。 – dysania 2012-01-16 20:17:04

+0

'add'确实存在错误,请参阅我的答案(或@ Chnoch's)。 – 2012-01-16 20:28:12

1

我想我已经找到了你的问题。 如果使用append(),则直接在尾部后面添加它。但是,如果您在尾部之后添加了前一个节点,则不会将尾部设置为新节点。这意味着,一旦你调用append()两次,你将放弃第一次追加()后添加的所有节点。

简单的例子:

public static void main(String[] args) { 
    LinkedList list = new LinkedList(); 
    list.add("First add"); 
    list.append("First Append"); 
    list.add("Second add"); 
    list.prepend("First prepend"); 
    list.add("Third add"); 
    list.prepend("Second prepend"); 
    list.add("fourth add"); 
    list.append("Second Append"); 
    list.add("Fifth add"); 
    list.add("Sixth add"); 

    list.start(); 
    do { 
     System.out.println(list.get().toString()); 

    } while (list.next()); 
} 

输出:

Second prepend 
fourth add 
First prepend 
Third add 
First add 
First Append 
Second Append 

结论: “第二次加入” 丢失,以及 “五加” 和 “第六次加” 因为你的next()方法只要到达尾巴就停下来。如果最后添加新节点,则需要始终更新尾部。

希望这会有所帮助。 Cheers,Chnoch

+0

非常有帮助的感谢,将对此工作 – dysania 2012-01-16 20:27:49

5

add确实存在一个问题:当节点已经存在时,它不会更新tail。考虑的行动序列:

public void print() { 
    Node curr = this.head; 
    while(curr != null) { 
     System.out.println(curr.item); 
     curr = curr.next; 
    } 
} 

像这样::

LinkedList list = new LinkedList(); 
list.add("one"); 
list.add("two"); 
list.append("three"); 

如果你要那么使用该打印

list.print(); 

你会得到以下输出:

one 
three 

发生这种情况使用tail - 其中append依赖于 - 继续执行第二个add操作后指向第一个Node