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

时间:2022-09-05 19:51:38

Here is my insertion method for doubly linked list. It works but passing two parameters for the method seems cumbersome. Anyone can suggest a cleaner approach than this?

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

Code // Execution example MyLinkedList list = new MyLinkedList(); Link link1 = new Link("A"); list.insert(list.head,link1); Link link2 = new Link("B"); list.insert(link1, link2); Link link3 = new Link("C"); list.insert(link2, link3); list.display_List_Start_From_Head();

代码//执行示例MyLinkedList list = new MyLinkedList();链接link1 =新链接(“A”); list.insert(list.head,LINK1);链接link2 =新链接(“B”); list.insert(link1,link2);链接link3 =新链接(“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;
    }


}

2 个解决方案

#1


2  

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

}

BTW, rename your class because LinkedList is too similar to java.util.LinkedList

顺便说一句,重命名你的类因为LinkedList与java.util.LinkedList太相似了

#2


1  

If you are appending to the end of the LinkedList, then you can just set list.head.prev.next = newLink, newLink.next = list.head, newLink.prev = list.head.prev and list.head.prev = newLink.

如果要附加到LinkedList的末尾,则可以设置list.head.prev.next = newLink,newLink.next = list.head,newLink.prev = list.head.prev和list.head.prev = NEWLINK。

#1


2  

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

}

BTW, rename your class because LinkedList is too similar to java.util.LinkedList

顺便说一句,重命名你的类因为LinkedList与java.util.LinkedList太相似了

#2


1  

If you are appending to the end of the LinkedList, then you can just set list.head.prev.next = newLink, newLink.next = list.head, newLink.prev = list.head.prev and list.head.prev = newLink.

如果要附加到LinkedList的末尾,则可以设置list.head.prev.next = newLink,newLink.next = list.head,newLink.prev = list.head.prev和list.head.prev = NEWLINK。