带链表的泛型方法

jmo0nnb3  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(242)

嗨,我正在努力使这两种方法发挥作用,但即使我尝试了几乎所有我能想到的方法,它仍然不起作用。请告诉我如何修理它!
void add(anything value):将包含newvalue的节点添加到列表的末尾。
void addafter(int index,anything value):在索引的节点之后添加一个包含newvalue的节点(假设索引从0开始)。
这是我的代码:(上面的方法显示在底部)

public class Node<Anything>
{

private Anything data;
private Node next;

Node(Anything a, Node<Anything> n)
{
    data = a;
    next = n;
}

public Anything getData()
{ 
    return this.data;
}

public Anything setData(Anything newData)
{
    Anything oldData = this.data;
    this.data = newData;
    return oldData;
}

public void setNext(Node<Anything> newNext)
{
    this.next = newNext;
}

public Node<Anything> getNext()
{
    return this.next;
}
 }

------------------------------------------

public class CS2LinkedList<Anything>
{  

private Node<Anything> first;
private Node<Anything> last;

public CS2LinkedList()
{
    first = null;
}

public boolean isEmpty()
{
    return (first == null);
}

public void addFirst(Anything d)
{
     Node<Anything> temp = first;
     first = new Node<>(d,temp);
}

public void clear()
{
    first = null;
}

public boolean contains(Anything value)
{
    for (Node curr = first; curr != null; curr = curr.getNext())
    {
        if (value.equals(curr.getData())){
            return true;
        }
    }
    return false;
}

public String toString()
{
    StringBuilder result = new StringBuilder();  //String result = "";
    for (Node curr = first; curr != null; curr = curr.getNext())
        result.append(curr.getData() + "->");  //result = result + curr.data + "->";
    result.append("[null]");
    return result.toString();   //return result + "[null]";
}

public int size()
{   
    int size = 0;
    for (Node curr = first; curr != null; curr = curr.getNext()){
         size++;
         if (first==null){
                 size = 0;
            }
        }
        return size;
         }

public Anything getFirst()
{   

    if (first!=null){
        return first.getData();
    }
    else{
       System.out.println("Sorry, the list is empty.");
       return null;
    }

}

public Anything getLast()
{
    if (first!= null){

        for(Node curr = first; curr != null; curr = curr.getNext()){
            first = curr;
        }
        return first.getData();
        //FIX: list2's size decreases by 1 after executing list2.getLast()
    }
    else{
        System.out.println("Sorry, the list is empty.");
        return null;
    }
}

public void add(Anything value){
    if (first==null){
        first = new Node<>(value,first);
    }

    Node<Anything> next = new Node<>(value, first);
    first.setNext(null);
    last = next;
 }

public void addAfter(int index, Anything value)
{
    return;
}
 }
gt0wga4j

gt0wga4j1#

回答有点长,试图解释每一个问题并给出解决方案。耐心点!
您所看到的代码正是按照您告诉它的方式进行的,以相反的方式添加新节点。它正在添加 new node 作为 first 设定 current first 作为新节点的 next 虽然 addFirst() 被称为。
但在当前情况下 add() 方法,您将始终得到一个不超过 one 通过调用 printing toString() 方法,尽管列表中包含的节点不超过两个。让我们看看您的代码以了解这一点:

public void add(Anything value){
    if (first==null){
        first = new Node<>(value,first);
    }

    Node<Anything> next = new Node<>(value, first);
    first.setNext(null);
    last = next;
}

所以你要做的是,你要检查 first 为空,表示当前列表是否为空。如果 true 然后初始化 first 具有新的价值(例如 "x" )下一个是 current first ,即 null . 所以我们得到了 [first(x)] -> [null] 作为我们目前的名单。
接下来,您将初始化一个 nextvaluefirst 因为它是下一个。然后,您将设置下一个 first 作为 null 分配 nextlast . 到目前为止,我们已经:

[next(x)] -> [first(x)] -> [null]        //where last node is next(x)

现在如果你想 add(y) 另一个 next 将使用当前 first 作为此新节点的下一个节点。然后再次重复制造 nullnextfirst . 然后再次将此新节点分配给 last . 现在我们有:

[next(y) -> [first(x)] -> [null]

好了,我们开始吧!你正在失去信心 current last 通过将新节点指定给 last ,还有这个 last 将有 first 下一步,总是要添加多少节点,它遵循相同的路径。
因此,您将得到一个 two 元素,总是这样 add() 方法。
但是为什么tostring()只提供 [first(x)] -> [null] 作为链接列表?
好吧,看看这个 toString() 功能,它从 first ,直至 first.next == null . 而且,你有 first.next == null ,始终,如果使用此选项添加节点 add() 方法。因此,政府的这种行为 toString() .
解决办法是什么?
设置两个 firstlast 初始化期间,构造函数中的链接列表的值为null。在 add() 方法,初始化 first 第一次与 valuelast . 并将其分配给 last . return 之后,我们将添加第一个节点。移除 first.setNext(null) 语句,初始化 new node 有价值 null . 使 nextcurrent last 作为 new node . 并将新节点指定为最后一个节点。代码如下:

public void add(Anything value) {
    if (first == null) {
        first = new Node<>(value, last);
        last = first;
        return;
    }

    Node<Anything> next = new Node<>(value, null);
    last.setNext(next);
    last = next;
}

现在如果你打印 toString() ,您将获得:

node1->node2->node3->[null]

希望你得到你的答案和想要的解决方案!

相关问题