需要帮助将一个链表添加到另一个链表

时间:2015-03-04 21:38:59

标签: java data-structures linked-list

我正在尝试将一个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行中的Node。我理解使用标题和游标时的理论我只是将它付诸实践。

2 个答案:

答案 0 :(得分:3)

当setNext仅接受节点时,您正尝试传递列表。这正是您收到错误的原因 您需要做的是将setNext传递给要合并的列表的头节点(l2)。

public void addList(LinkedList<T> list)
{
    Node current = head;

    while (current.getNext() != null)
    {
        current = current.getNext();
    }
    current.setNext(list.getFirst()); //Try this

}

答案 1 :(得分:0)

而不是:

current.setNext(list);

使用:

current.setNext(list.getFirst());