【发布时间】:2015-03-04 21:38:59
【问题描述】:
我正在尝试将一个 LinkedList 添加到另一个 LinkedList。在过去的一两个小时里,我一直在尝试这样做,但我束手无策。这是我试图在 main 中运行的行。
l.addList(l2); //l is the original list and l2 is the list I am trying to add, l2 does have elements in it.
这是 LinkedList 类:
public class LinkedList<T>
{
private Node<T> head; // head of the list always at the front
private Node<T> cursor; // cursor that moves along the one way list
// constructor
public LinkedList ()
{
// the first node is not used, dummy node
// so we're always dealing with the element to the right of
// the cursor not what the cursor is pointing to.
head = new Node<T>(null, null);
cursor = head;
}
// if the cursor's next is null, then we're at the end
public boolean isAtEnd()
{
return(cursor.getNext() == null);
}
// move the cursor to the beginning of the list
public void reset()
{
cursor = head;
}
// advance the cursor one spot to the right
public void advance()
{
cursor = cursor.getNext();
}
// return the node to the right of the cursor
public Node<T> getCurrent()
{
return cursor.getNext();
}
// return the first node in the list
public Node<T> getFirst()
{
return head.getNext();
}
public void addList(LinkedList<T> list)
{
Node current = head;
while (current.getNext() != null)
{
current = current.getNext();
}
current.setNext(list);
^error here
}
// insert at the beginning of the list, this insert is done to the
// right of the dummy node, but to the left of the first meaningful
// node.
public void listHeadInsert(T value)
{
head.setNext(new Node<T>(value, head.getNext()));
}
// wherever the cursor is, insert to the right of it, and move the
// cursor to point to the newly inserted node
// you may remove the line that advances the cursor, but you need
// to make sure that you advance the cursor when inserting elements
// at the end of the list one after another.
public void listInsert(T value)
{
// insert to the right of the cursor
cursor.setNext(new Node<T>(value, cursor.getNext()));
cursor = cursor.getNext();
}
// move the cursor to the head of the list, and keep moving it
// looking for the value, stop if you either find the value
// or you have reached the end of the list without finding it.
// return the node that contains the given value back to me.
// this return will return null if the value is not found.
public Node<T> listSearch(T value)
{
cursor = head;
while(cursor.getNext() != null &&
!cursor.getNext().getValue().equals(value))
cursor = cursor.getNext();
return cursor.getNext();
}
// first search (first 4 lines of the code)
// if you find it (not null) then just remove it by making the
// cursor's next pointer point to the node next to it's next
// pointer (skip a node)
public void listRemove(T value)
{
cursor = head;
while(cursor.getNext() != null &&
!cursor.getNext().getValue().equals(value))
cursor = cursor.getNext();
if(cursor.getNext() != null)
{
cursor.setNext(cursor.getNext().getNext());
}
}
// don't search, just remove the node to the right of the cursor
// if it's not null.
public void listRemoveCurrent()
{
if(cursor.getNext() != null)
{
cursor.setNext(cursor.getNext().getNext());
}
}
}
这是 Node 类:
// Node of any Reference type T
public class Node<T>
{
private T value; // this is the data value
private Node<T> next; // this is pointing to the next node
// the node constructor
public Node (T v, Node<T> n)
{
value = v;
next = n;
}
// getters and setters for the node's value and next pointer
public T getValue() {return value;}
public Node<T> getNext() {return next;}
public void setValue(T v){value = v;}
public void setNext(Node<T> n){next = n;}
}
我收到错误:不兼容的类型,LinkedList 无法转换为类 LinkedList 中的节点,第 69 行。我理解使用标题和游标时的理论我只是在将其付诸实践时遇到问题。
【问题讨论】:
-
与您的错误无关,您的
getFirst()和您的getCurrent()方法都是错误的。您将从getFirst()获得第二个 项,以及getCurrent()中光标之后 的项。 -
@RealSkeptic 基于代码中的 cmets,在这个链表实现中,头节点是一个虚拟节点。
标签: java data-structures linked-list