小编Lar*_*ere的帖子

add()方法用于链表

我正在尝试创建一个方法,将一个节点添加到链表,但到目前为止一直没有成功.这是我的代码与我的成员vars:

private Object data; 
private int size = 0;
private Node head = null; 
private Node tail = null;

    public void add(Object item){
    Node temp = head;
    if (head != null) {
        // THIS IS THE PROBLEM SITE

        while(temp.getNext()!=null){
            temp=temp.getNext();
        }
        //set next value equal to item
        Node ab = (Node) item; // It says this is an invalid cast. How do I get around this??
        ab.setNext(ab);

    } 
    else{
        head = new Node(item);
    }
    size++;
}
Run Code Online (Sandbox Code Playgroud)

这里也是我的Node类供参考:

public class Node …
Run Code Online (Sandbox Code Playgroud)

java methods linked-list add nodes

0
推荐指数
1
解决办法
462
查看次数

标签 统计

add ×1

java ×1

linked-list ×1

methods ×1

nodes ×1