如何在Java中使用带有通用节点的Cyclic Doubly-Linked-List编写一个remove方法

问题描述 投票:0回答:1

我正在实现循环DoublyLinkedList数据结构。与单链表一样,双链表中的节点具有对下一节点的引用,但与单链表不同,双链表中的节点也具有对前一节点的引用。此外,因为列表是“循环”的,所以列表中最后一个节点中的“下一个”引用指向列表中的第一个节点,列表中第一个节点中的“prev”引用指向最后一个节点。列表。

我的删除方法有一些大小用法我遇到了问题。这是我运行测试时得到的信息。

这是我的代码:

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 (first.data == null)
    {
        throw new IndexOutOfBoundsException();
    } else if (index == 0)
    {
        first = new Node(value, first.next, first.prev);
    }
    else
        {
        Node current = first;
        for (int i = 0; i < index - 1; i++)
        {
            current = current.next;
        }
        current.next = new Node(value, current.next, current.prev);
    }
}

这是我需要帮助的方法。 remove方法应该删除列表中指定索引处的元素。请务必解决列表为空和/或删除的元素是列表中第一个的情况。如果index参数无效,则应抛出IndexOutOfBoundsException。

@SuppressWarnings("unchecked")
public void remove(int index)
{
    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 - 1; i++)
        {
            current = current.next;
        }--size;
            current.next = current.next.next;

    }
}

这是代码的其余部分。 get方法不正确,但我在另一个问题中问过。

    public E get(int index)
    {
   if(index >= size)
    {

    }
    return null;
    //return first.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;
}
java generics nodes doubly-linked-list remove-method
1个回答
0
投票

这根本不容易,但我确实找到了我的问题的答案。这是一个循环的双向链表。这里是:

 @SuppressWarnings("unchecked")
 public void remove(int index)
 {
    if(index < 0 || index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    Node n = first;
    for(int i = 0; i < index; i++)
    {
        n = n.next;
    }
    // n points to node to remove
    n.prev.next = n.next;
    n.next.prev = n.prev;
    if (index == 0)
    {
        if(size == 1)
        {
            first = null;
        }
        else
        {
            first = first.next;
        }
    }
    size--;
}
© www.soinside.com 2019 - 2024. All rights reserved.