gpt4 book ai didi

java - 需要帮助将一个链接列表添加到另一个链接列表

转载 作者:行者123 更新时间:2023-12-01 22:25:29 24 4
gpt4 key购买 nike

我正在尝试将一个 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 类中的 Node,第 69 行。我理解使用 header 和游标时的理论,但在实践中遇到问题。

最佳答案

当 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

}

关于java - 需要帮助将一个链接列表添加到另一个链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com