链表的通用方法

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

试图让这两种方法发挥作用,但我对泛型没有太多经验,而且这个概念让人很困惑。
anything getfirst():返回存储在列表中第一个节点中的值。它应该打印一条错误消息,如果列表为空,则返回null。
anything getlast():返回存储在列表中最后一个节点中的值。它应该打印一条错误消息,如果列表为空,则返回null。
这是我的代码:(上面的方法显示在底部)

public class Node<Anything>{

private Anything data;
private Node next;

Node(Anything a, Node 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 newNext)
{
    this.next = newNext;
}

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

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

public class CS2LinkedList<Anything>{  

private Node first;
private Node last;

public CS2LinkedList()
{
    first = null;
}

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

public void addFirst(Anything d)
{
     Node 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;
         }

// ------------------------ Question begins here ------------------------

public Anything getFirst()
{   
    if (first != null){
        // What should I return here? I tried returning first, (Anything) first, but none of them seems to work.
    }
    else{
        return null;
    }
}

public Anything getLast()
{
    if (first != null){
        // Same here
    }
    else{
        return null;
}
 }
yizd12fk

yizd12fk1#

班级 Node 有一个类型参数,但在您的类中 CS2LinkedList 您使用它时不带类型参数。你把它当作原始类型使用。原始类型只存在于java中,用于向后兼容非常旧的java版本,而这些版本没有泛型。您不应该使用原始类型(除非绝对必要,因为您必须使用非常旧的代码)。
无论你在哪里写作 Node 在你们班 CS2LinkedListNode<Anything> 相反例如,如下所示声明成员变量:

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

写你的 addFirst 方法如下:

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

等等。
然后,你可以写你的 getFirst() 方法如下:

public Anything getFirst()
{   
    if (first != null){
        return first.getData();
    }
    else{
        return null;
    }
}

和其他类似的情况 getLast() 方法。
您还需要修改类中的一些代码 Node . 构造函数参数和 setNext 方法还应具有类型参数:

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

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

以及 getNext 方法:

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

相关问题