如何在 Java 中使用 DoublyLinkedList 编写 add/insert 方法

How to write an add/insert method with a DoublyLinkedList in Java

我在 java 中的添加方法需要帮助。它适用于双向链表。

我正在实现循环 DoublyLinkedList 数据结构。就像一个人 链表,双向链表中的节点引用下一个节点,但与单链表不同,双向链表中的节点也引用前一个节点。另外,因为列表是"cyclic",所以列表最后一个节点中的"next"引用指向列表中的第一个节点,而列表中第一个节点中的"prev"引用指向列表中的最后一个节点。

该方法应该做的是在列表中的指定索引处插入值参数。请务必解决列表中的情况 为空 and/or 添加的元素是列表中的第一个。如果索引参数无效,则应抛出 IndexOutOfBoundsException。

下面是我的代码:

public class DoublyLinkedList<E>
{
private Node first;
private int size;

@SuppressWarnings("unchecked")
public void add(E value)
{

    if (first == null)
    {
        first = new Node(value, null, null);
        first.next = first;
        first.prev = first;
    }
    else
        {
        first.prev.next = new Node(value, first, first.prev);
        first.prev = first.prev.next;
    }
    size++;
}
private class Node<E>
{
    private E data;
    private Node next;
    private Node prev;

    public Node(E data, Node next, Node prev)
    {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }
}

这是失败的方法。我将评论我所坚持的那一行,但除此之外,根据我所听到的,在前面几行中所做的是正确的。

@SuppressWarnings("unchecked")
public void add(int index, E value)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    if (first.data == null)
    {
        throw new NullPointerException();
    }
    if (index == 0)
    {
        first = new Node(value, null, null);
        first.next = first;
        first.prev = first;
    }
    else
        {
        Node current = first;
        for (int i = 0; i < index; i++)
        {
            current = current.next;
        }
        current.prev.next = new Node(value, current, current.prev); // This is the line where I get lost on. 
        current.prev = current.prev.next;
    }
    size++;
}

我的其余代码在这里。请关注我正在研究的方法。谢谢你!

@SuppressWarnings("unchecked")
public void remove(int index)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    if (first.data == null)
    {
        throw new IndexOutOfBoundsException();
    }
    else if (index == 0)
    {
        first = first.next;
    }
    else
        {
            Node current = first.next;
            for (int i = 0; i < index; i++)
        {
            current = current.next;
        }
           // current.prev = current.next;
            current.next = current.next.next;
    }
    size--;
}
@SuppressWarnings("unchecked")
public E get(int index)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    Node current = first;
    for (int i = 0; i < index; i++)
    {
        current = current.next;
    }
    return (E) current.data;
}
@SuppressWarnings("unchecked")
public int indexOf(E value)
{
    int index = 0;
    Node current = first;
    while (current != current.next)
    {
        if (current.data.equals(value))
        {
            return index;
        }
        index++;
        current = current.next;
    }
    return index;
}
public boolean isEmpty()
{
    if (size == 0)
    {
        return true;
    }
    else
        {
        return false;
    }
}
public int size()
{
    return size;
}
@SuppressWarnings("unchecked")
public String toString()
{
    if (isEmpty())
    {
        return "[]";
    }
    else
        {
            String result = "[" + first.data;
            Node current = first.next;
        for(int i = 0; i < size-1; i++)
        {
            result += ", " + current.data;
            current = current.next;
        }
        result += "]";
        return result;
    }
}
}

这一点都不容易,但我找到了问题的答案。

 @SuppressWarnings("unchecked")
 public void add(int index, E value)
 {
    if(index > size || index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if (first == null)
    {
        Node n = new Node(value, null, null);
        n.next = n;
        n.prev = n;
        first = n;
    }
    else
        {
        Node current = first;
        for (int i = 0; i < index; i++)
        {
            current = current.next;
        }
        //current points to node that will follow new node.
        Node n2 = new Node(value, current, current.prev);
        current.prev.next = n2;
        current.prev = n2;
        //update first if necessary.
        if(index == 0)
        {
            first = n2;
        }
    }
    size++;
}