如何在Java中实现双链表?

如何在Java中实现双链表?,java,linked-list,Java,Linked List,对于具有插入、删除和替换功能的java,实现双链表的最佳方式是什么 除非这是家庭作业(在这种情况下,您应该将其标记为家庭作业),否则很难做得比这更好: class MyLinkedList<T> extends java.util.LinkedList<T> { } 类MyLinkedList扩展了java.util.LinkedList{ } 从文档中: 所有操作的执行都与双链接列表的预期相同。索引到列表中的操作将从开始或结束遍历列表,以更接近指定索引的为准 除非这

对于具有插入、删除和替换功能的java,实现双链表的最佳方式是什么

除非这是家庭作业(在这种情况下,您应该将其标记为家庭作业),否则很难做得比这更好:

class MyLinkedList<T> extends java.util.LinkedList<T> {
}
类MyLinkedList扩展了java.util.LinkedList{
}
从文档中:

所有操作的执行都与双链接列表的预期相同。索引到列表中的操作将从开始或结束遍历列表,以更接近指定索引的为准

除非这是家庭作业(在这种情况下,你应该给它贴上这样的标签),否则很难做得比这更好:

class MyLinkedList<T> extends java.util.LinkedList<T> {
}
类MyLinkedList扩展了java.util.LinkedList{
}
从文档中:

所有操作的执行都与双链接列表的预期相同。索引到列表中的操作将从开始或结束遍历列表,以更接近指定索引的为准


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

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

不要自己实现,请使用。不过,我假设这是一个家庭作业问题,因此您可以查看LinkedList的源代码。

不要自己实现,请使用。然而,我认为这是一个家庭作业问题,因此也许您可以查看LinkedList的源代码。

如果您对双链接列表和其他一些数据结构的实现方式感兴趣,我建议您阅读Duane Bailey关于数据结构的书。可通过以下网址免费下载:


这是一本相当直截了当的书,他展示了如何实现所有不同的数据结构——有一节完全涵盖了您的问题。我发现它对我研究数据结构及其工作方式非常有帮助;我希望您也能发现它的帮助。

如果您对双链表和其他一些数据结构的实现方式感兴趣,我建议您阅读Duane Bailey关于数据结构的书。可通过以下网址免费下载:

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();
    }
}


这是一本相当直截了当的书,他展示了如何实现所有不同的数据结构——有一节完全涵盖了您的问题。我发现它对我研究数据结构及其工作方式非常有帮助;我希望你也会觉得它很有用。

通过学习链表的工作原理或实现方式,这绝对没有任何作用。这是真的。但我并不是这样解释这个问题的。他问起如何实现它,因此人们会认为他不仅仅是想继承另一个实现。我认为如果mTuran想学习如何实现一个链表,他可以用谷歌搜索一下。@Reese,我为他提供了一种实现它的方法。同时,我还含蓄地说,这听起来像是他想要的
java.util.LinkedList
。但是,通过学习链表的工作原理或实现方式,这绝对没有任何作用。这是真的。但我并不是这样解释这个问题的。他问起如何实现它,因此人们会认为他不仅仅是想继承另一个实现。我认为如果mTuran想学习如何实现一个链表,他可以用谷歌搜索一下。@Reese,我为他提供了一种实现它的方法。与此同时,我含蓄地说,这听起来像是他想要的
java.util.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();
    }
}