【问题标题】:How to find the size of a generic singly-linked list in java如何在java中查找通用单链表的大小
【发布时间】:2014-01-11 19:29:33
【问题描述】:

我无法在 java 中找出通用单链表的大小。这是我的代码(下面我将解释我是如何找到它的以及遇到了什么困难):

class List<T> {
/**
 * An immutable singly-linked list.
 * @param <T> the type of the list elements
 */
T head;
List<T> tail;

List(T head, List<T> tail) {
    this.head = head;
    this.tail = tail;
}

/** Generic helper function for list creation. You DO NOT NEED to call this function. */
static <U> List<U> node(U head, List<U> tail) {
    return new List<U>(head, tail);
}


/* Getters and Setters for the head and the tail; */
public List<T> getTail() {
    return tail;
}

public void setTail(List<T> tail) {
    this.tail = tail;
}

public T getHead() {
    return head;
}

public void setHead(T head) {
    this.head = head;
}

我试图找出这样的大小: 循环遍历链表的元素,从第一个元素开始,直到“next”指针显示null。增加辅助变量size。 代码:

    public int size(){
    int size = 0;

    T temp = head;
    while (temp.getTail() != null) {
        temp = temp.getTail();
        size++;
    }

    return size;
}

问题是temp.getTail()。在这种特定情况下,Eclipse 要求将变量temp 转换为List&lt;T&gt;。但这对我来说毫无意义,因此List&lt;T&gt; 应该就像指向列表下一个元素的“下一个”指针。

请有人这么好心并解释我做错了什么以及如何解决这个问题。我真的很想了解泛型(我也阅读了很多关于泛型的文章,但似乎仍然无法弄清楚如何处理这种情况)。

我将在我的测试类中使用这个列表:

        List<Integer> coins = List.node(1, List.node(2, List.node(5,  List.node(10,
            List.node(20, List.node(50, List.node(100,  List.node(200, null))))))));

在给定欧元金额的情况下,我将递归计算可能的硬币组合的数量(值 1、2、5、10、20、50、100 和 200)。

【问题讨论】:

  • 为什么tail的类型是List&lt;T&gt;?此外,getTemp() 方法在List&lt;T&gt; 中定义,并且您正试图从T 类型变量中调用它。那是行不通的。
  • @RohitJain 这是一个课堂作业。类List&lt;T&gt;的属性给了我们。我也想弄清楚为什么tail 的类型是List&lt;T&gt; 而不仅仅是T。我的猜测是,tail 是某种“下一个”指针,它显示到列表的下一个元素。在这种情况下,如何遍历列表的元素以找出大小?这是我的问题...

标签: java generics linked-list


【解决方案1】:

应该是:

int size = 1;

List<T> temp = this;

while (temp.getTail() != null) {
    temp = temp.getTail();
    size++;
}

T 没有尾巴,但 List&lt;T&gt; 有,这就是你的温度应该是什么。

假设它是List&lt;Integer&gt;,那么您的代码将如下所示:

Integer temp = head;
while (temp.getTail() != null) {
    temp = temp.getTail(); 
    size++;
}

tempInteger,而尾部将是 List&lt;Integer&gt;,因此您不能将一整串整数分配给 Integer

【讨论】:

  • 您遇到了一个错误。但除此之外它很好(与我的迭代相同),所以请点赞!
  • @vandale 非常感谢您的快速响应。但我有个问题。为什么从 1 而不是从 0 开始计数的递增?据我所知,列表的第一个元素的索引为 0。
  • @user3185735 假设您有一个列表。这意味着该列表将至少包含一个元素,因此您从 1 开始。然后每个额外的元素都会增加 1。把它想象成一个数组:它的最后一个元素将在 length-1 但是 length
  • @vandale 现在对我来说非常有意义:) 非常感谢您的帮助。你们太棒了!
【解决方案2】:

T 定义在List&lt;T&gt; 中时,您正尝试从T 调用getTail()

一个可行的迭代解决方案是:

public int size(){
    int size = 1;

    List<T> temp = this;
    while (temp.getTail() != null) {
        temp = temp.getTail();
        size++;
    }

    return size;
}

虽然在 Java 中可能不建议使用递归解决方案,但可能更符合您的递归数据结构:

public int size() {
    if (getTail() == null)
        return 1;
    else
        return 1 + getTail().size();
}

【讨论】:

  • 非常感谢。这和@vandale 的回应已经达成了交易:)。不过只有一个问题......为什么在这种情况下我们需要T head,因为我们不在此属性中存储数据?对此有何意见?
  • head 是数据。例如,如果您想打印列表,您将递归或迭代并打印每个条目中的 head 字段。
  • 非常感谢 :) 你帮我解决了我的下一个问题,从列表的特定索引中获取数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多