简体   繁体   English

如何在 Java 中实现双向链表?

[英]How do I implement a Doubly Linked List in Java?

具有插入,删除和替换功能的java双向链表实现的最佳方法是什么?

Unless this is homework (in which case you should tag it as such), it would be hard to do any better than this:除非这是作业(在这种情况下您应该将其标记为这样),否则很难做得比这更好:

class MyLinkedList<T> extends java.util.LinkedList<T> {
}

From the docs:从文档:

All of the operations perform as could be expected for a doubly-linked list .所有操作的执行都符合双向链表的预期。 Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.索引到列表中的操作将从开始或结束遍历列表,以更接近指定索引的为准。

有一个称为Node的私有内部类,它表示列表中的数据,它有一个下一个节点、一个前一个节点和一个数据值,以及获取和设置每个节点的方法。

If you are interested in how doubly-linked lists and some other data structures are implemented, I would recommend looking over Duane Bailey's book on data structures.如果您对双向链表和其他一些数据结构的实现方式感兴趣,我建议您阅读 Duane Bailey 的关于数据结构的书。 It's available as a free pdf at:它可以在以下位置以免费 pdf 格式获得:

http://www.cs.williams.edu/~bailey/JavaStructures/Book.html http://www.cs.williams.edu/~bailey/JavaStructures/Book.html

It's a fairly straight-forward book, and he shows how all the different data structures can be implemented - there's a section that thoroughly covers your question.这是一本相当直接的书,他展示了如何实现所有不同的数据结构 - 有一个部分彻底涵盖了您的问题。 I found it very helpful in my studies of data structures and how they work;我发现它对我研究数据结构及其工作方式很有帮助; I hope you find it helpful too.我希望你也觉得它有帮助。

java.util.LinkedList<E> 已经双重链接,如果它不符合您的需要,您可以检出/修改源。

Don't implement it yourself, use LinkedList .不要自己实现它,使用LinkedList However I assume this is some sort of homework problem so perhaps you could look at the source code to LinkedList.但是,我认为这是某种作业问题,因此也许您可以查看 LinkedList 的源代码。

public class DoublyLL {

class Node{
    int data;
    Node prev;
    Node next;

    Node(int d)
    {
        data=d;
    }
}
Node head;

public void fadd(int data)//Adding at first
{
    Node n=new Node(data);
    n.next=head;//Making the new node as head
    n.prev=null;//assignig new node's prev value as null so that it becomes new head
    if(head!=null)
    {
        head.prev=n;//changing prev of head node from null to new node
    }
    head=n;//head pointing toward new node
}

public void ladd(int data) //adding at last
{
    Node n=new Node(data);
    Node last=head;  //Make the node at last as head
    n.next=null;    //Point the new node next value as null so that it becomes new 
//tail
    if(head==null)  //if the LL is empty then make the new node as hhead
    {
        n.prev=null; 
        head=n;
        return;
    }
    while(last.next!=null)  //while last is pointing as null
    {
        last=last.next;     
    }
    last.next=n;    //next value of last is pointing as null to become new tail
    n.prev=last; //and prev value is pointing toward last Node
 }

  public void delete(Node n,Node d) //deletion of node at head
  {
    if(head==null || d==null) //when the node to be deleted is null
    {
        return;
    }
    if(head==d)     //if the node to be deleted iss head
    {
        head=d.next;    //point thhe head towards new head
    }
    if(d.next!=null)    //Change next only if node to be deleted is NOT the last node
    {
        d.next.prev=d.prev;
    }
    if(d.prev!=null)    // Change prev only if node to be deleted is NOT the firstnode
    {
        d.prev.next=d.next;
    }
    return;
  }

 public void disp() //traversing
    { 
        Node curr=head;
    Node last=null; 
    while (curr!=null)
        {
        System.out.print(curr.data + " "); 
        last=curr; 
        curr=curr.next; 
        }
    System.out.println(); 
    } 

public static void main(String[] args)
{
    DoublyLL dl=new DoublyLL();
    dl.fadd(1);
    dl.fadd(131);
    dl.fadd(21);
    dl.fadd(22);
    dl.disp();
    dl.ladd(12);
    dl.disp();
    dl.ladd(2);
    dl.delete(dl.head,dl.head);
    dl.disp();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM