单向链表 - get 和 add 方法

Singly Linked List - get and add methods

所以我正在尝试通过完成实现来实现 SLList class:

get(i)set(i, x)add(i, x)remove(i) 操作,每个 运行 需要 O(1 + i) 时间。

我的程序遇到的困难是添加和获取方法。我不断收到错误 incompatible types: SLList<T>.Node cannot be converted to intincompatible types: SLList<T>.Node cannot be converted to int.

我很困惑如何修复它们。我今天刚了解链表,我正在努力掌握它们的概念。非常感谢任何帮助或提示。

public T get(int i) {
    // TODO: Implement this
    Node u = head;
    for(int j = 0; j < i; j++){
        i = u.next;
    }
    return u;
    if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
    return null;
}


public void add(int i, T x) {
        Node u = new Node();
        u.x = x;
        if (i == 0) {
            head = u;
        } else {
            tail.next = u;
        }
        tail = u;
        i++;
        return true;
        if (i < 0 || i > n) throw new IndexOutOfBoundsException();
}

我应该提到每个函数 T 和 void 的类型必须保持原样。我也相信我应该在我的代码中包含 IndexOutOfBoundsException 部分。

如果你们想在此处查看我的完整代码:https://pastebin.com/nJ9iMjxj

在您的 node class 中,next 的类型是 node,而在您的 get 方法中,您正在分配一个 node 到整数变量 :

i = u.next;

我没有看到你的整个实现,但我认为它应该是 u = u.next;

在您的 get 方法中,您试图在第 5 行将 Node 分配给 int 变量。相反,您应该编写 u=u.next;