用于在双向链表中插入的更好方法结构 (Java)

Better method structure for insertion in doubly linked list (Java)

这是我的双向链表插入方法。它有效,但为该方法传递两个参数似乎很麻烦。任何人都可以提出比这更清洁的方法吗?

代码 // 执行示例 我的LinkedList 列表 = 新的我的LinkedList(); Link link1 = new Link("A"); list.insert(list.head,链接1); Link link2 = new Link("B"); list.insert(链接1,链接2); Link link3 = new Link("C"); list.insert(link2, link3); list.display_List_Start_From_Head();

public void insert(Link currentNode, Link newNode)
    {
        if(head == null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            currentNode.next = newNode;
            newNode.prev = currentNode;
            tail = newNode;
        }


    }


public class MyLinkedList {

    public Link head;
    public Link tail;
    public Link currentNode;
    public Link newNode;

    public MyLinkedList()
    {
        head = null;
        tail = null;
    }


public class Link {

    public String data;
    public Link prev;
    public Link next;

    public Link(String data)
    {
        this(data,null,null);
    }

    public Link(String data, Link prev, Link next)
    {
        this.data = data;
        this.prev = prev;
        this.next = next;
    }


}

如果您要附加到 LinkedList 的末尾,那么您可以只设置 list.head.prev.next = newLinknewLink.next = list.headnewLink.prev = list.head.prevlist.head.prev = newLink

public void insert(Link newNode)
{
    if(head == null)
    {
        head = newNode;
        tail = newNode;
    }
    else
    {
        tail.next = newNode;
        newNode.prev = tail;
        tail = newNode;
    }

}

顺便说一句,重命名您的 class,因为 LinkedListjava.util.LinkedList

太相似了