Java 中的 LinkedList 实现,具有泛型和增强功能

发布于 2024-09-25 04:08:06 字数 1741 浏览 4 评论 0原文

我需要您检查一下我对单链表 (SLL) 的实现。实现应该使用泛型并能够使用增强的 for。

问题是,当我将 for (Number n : list) 作为 list 时,MyLinkedListMyLinkedList;,我收到错误:“类型不匹配:无法从元素类型对象转换为数字”。

这就是我所拥有的。我不太确定的部分是泛型和迭代器。

提前致谢。

import java.util.Iterator;

public class MyLinkedList<T> implements Iterable<Object>
{
    private Node head;

    public MyLinkedList ()
    {
        head = null;
    }

    public void add (Node n)
    {
        if (head == null)
        {
            head = n;
        }

        else
        {
            Node node = head;
            while (node.next != null) 
            {
                node = node.next;
            }
            node = n;
        }
    }

    public Iterator iterator() 
    {
        return new MyLinkedListIterator (head);
    }

    public int size () 
    {
        int ret = 0;
        MyLinkedListIterator it = new MyLinkedListIterator (head);
        while (it.hasNext ())
        {
            it.next();
            ret++;
        }

        return ret;
    }

    public Node getHead ()
    {
        return head;
    }
}

class MyLinkedListIterator<T> implements Iterator
{
    private Node node;

    public MyLinkedListIterator (Node h)
    {
        node = h;
    }

    public MyLinkedListIterator (MyLinkedList<T> l)
    {
        this(l.getHead ());
    }

    public boolean hasNext () 
    {
        if (node.next == null)
        {
            return false;
        }

        else
        {
            return true;
        }
    }

    public Object next () 
    {
        return node.next;
    }

    public void remove () 
    {

    }   
}

I need you to review my implementation of a Singly Linked List (SLL) please. The implementation should use generics and be able to use the enhanced for.

The problem is that, when I do for (Number n : list) being list a MyLinkedList<Integer> or MyLinkedList<Double>, I get the error: "Type mismatch: cannot convert from element type Object to Number".

This is what I have. The parts I am not very certain about are the generics and the iterators.

Thanks in advance.

import java.util.Iterator;

public class MyLinkedList<T> implements Iterable<Object>
{
    private Node head;

    public MyLinkedList ()
    {
        head = null;
    }

    public void add (Node n)
    {
        if (head == null)
        {
            head = n;
        }

        else
        {
            Node node = head;
            while (node.next != null) 
            {
                node = node.next;
            }
            node = n;
        }
    }

    public Iterator iterator() 
    {
        return new MyLinkedListIterator (head);
    }

    public int size () 
    {
        int ret = 0;
        MyLinkedListIterator it = new MyLinkedListIterator (head);
        while (it.hasNext ())
        {
            it.next();
            ret++;
        }

        return ret;
    }

    public Node getHead ()
    {
        return head;
    }
}

class MyLinkedListIterator<T> implements Iterator
{
    private Node node;

    public MyLinkedListIterator (Node h)
    {
        node = h;
    }

    public MyLinkedListIterator (MyLinkedList<T> l)
    {
        this(l.getHead ());
    }

    public boolean hasNext () 
    {
        if (node.next == null)
        {
            return false;
        }

        else
        {
            return true;
        }
    }

    public Object next () 
    {
        return node.next;
    }

    public void remove () 
    {

    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧竹 2024-10-02 04:08:06
  • 您应该使用 Iterable 而不是 Iterable
  • add(Node) 实际上并不将对象添加到列表中。
  • MyLinkedListIterator 应实现 Iterator
  • 如果列表为空,MyLinkedListIterator.hasNext() 将抛出 NullPointerException
  • MyLinkedListIterator.next() 不会移动到列表中的下一项。
  • You should have Iterable<T> instead of Iterable<Object>.
  • add(Node) doesn't actually add an object to the list.
  • MyLinkedListIterator<T> should implement Iterator<T>.
  • MyLinkedListIterator.hasNext() will throw a NullPointerException if the list is empty.
  • MyLinkedListIterator.next() doesn't move to the next item in the list.
雪化雨蝶 2024-10-02 04:08:06

您应该从 iterator 方法返回一个 Iterator,并且还应该扩展 Iterable 而不是 Iterable;。

此外,您的 MyLinkedListIterator 应该实现 Iterator。那么它应该可以工作。

You should return an Iterator<T> from the iterator method and you should also extend Iterable<T> instead of Iterable<Object>.

Besides, your MyLinkedListIterator<T> should implement Iterator<T>. Then it should work.

好菇凉咱不稀罕他 2024-10-02 04:08:06

除了其他人所说的之外,您可能不应该在公共方法中公开 Node - 节点应该是实现的纯粹内部方面。

On top of what the others have said, you probably shouldn't be exposing Node in your public methods - nodes should be a purely internal aspect of the implementation.

孤单情人 2024-10-02 04:08:06

为什么不使用

public class Node<E>{
 E data;
 Node<E> next;
}

public class SinglyLinkedList<E> {
  
 Node<E> start;
 int size;
 .......
}

这里< /a> 全面实施

Why dont you use <E>

public class Node<E>{
 E data;
 Node<E> next;
}

public class SinglyLinkedList<E> {
  
 Node<E> start;
 int size;
 .......
}

Look here for a comprehensive implementation

溺渁∝ 2024-10-02 04:08:06

扩展要点:MyLinkedListIterator.next() 不会移动到列表中的下一项。

下一个方法应该是这样的,以使其正常工作:

public T next() {
    if(isFirstNode) {
        isFirstNode = false;
        return node.data;
    }
    node = node.next;
    return node.data;
}

Expanding the point: MyLinkedListIterator.next() doesn't move to the next item in the list.

the next method should be something along these lines to get it working:

public T next() {
    if(isFirstNode) {
        isFirstNode = false;
        return node.data;
    }
    node = node.next;
    return node.data;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文