繁体   English   中英

双链表实现

[英]Doubly Linked Lists Implementation

我在这里查看了有关双链表的大多数主题,但仍不清楚以下内容。

我正在用Java练习Goodrich和Tamassia的书。 关于双向链表,如果我错了,请纠正我,它与单链表的不同之处在于,可以在下一个节点和上一个节点同时将节点插入到任何位置,而不仅仅是在头尾之后。单链列表,无法在列表的任何位置插入?

如果要在双向链表中插入节点,则默认参数应该是要插入的节点之后的节点还是要插入的节点之前的节点? 但是,如果是这样,那么我不了解如何在此之前或之后传递节点。 我们是否应该显示到目前为止已插入的所有节点,并要求用户选择要在其中插入一些新节点的节点? 我的疑问是如何传递此默认节点。 因为我假设这也将需要这些节点的下一个和上一个节点。

例如, Head<->A<->B<->C<->D<->E<->tail

如果Z是在说D之后要插入的新节点,那么应该如何传递节点D? 我对此感到困惑,尽管对于大多数人来说这似乎很简单。

但是请解释一下。

谢谢,桑杰

假设这样的Node类:

public class Node {
    public Node next = null;
    public Node previous = null;
    public Object value;
}

然后可以定义:

public void insertBefore(Node node, Node insert)
{
    Node previousNode = node.previous;

    insert.next = node;
    insert.previous = previousNode;

    if(previousNode != null)
        previousNode.next = insert;

    node.previous = insert;
}

public void insertAfter(Node node, Node insert)
{
    Node nextNode = node.next;

    insert.previous = node;
    insert.next = nextNode;

    if(nextNode!= null)
        nextNode.previous = insert;

    node.next = insert;
}

希望能有所帮助

它与单链接列表的不同之处在于,可以使用下一个和上一个节点将节点插入到任何位置,而不是仅在头尾之后,而在单链接列表中,无法在列表中的任何位置插入节点?

错了 您可以在单链列表的中间插入节点。 唯一的区别是,双向链接列表更易于浏览,因为您可以从任何给定节点向前和向后浏览它。

如果要在双向链表中插入节点,那么默认参数应该是要插入节点之后的节点还是要插入节点之前的节点?

这完全取决于您使用列表的目的。 由于链接列表的随机访问性能较差,因此它并不是真正的通用数据结构。 因此,它们通常仅用于无论如何都要遍历列表并在执行操作时插入或删除节点的算法。

但是,如果是这样,那么我不了解如何在此之前或之后传递节点。 我们是否应该显示到目前为止已插入的所有节点,并要求用户选择要在其上插入一些新节点的节点?

您正以错误的方式来处理此问题,试图围绕某个数据结构编写应用程序。 通常,您从应用程序的最终用户需求开始,然后确定最适合用来满足那些需求的数据结构。 用户永远不必了解链接列表之类的程序的低级内部。

  1. 您也可以在单个链接列表中插入。 A-> B变为A-> C-> B,您要做的就是更改A的下一个节点引用并将C设置为B。
  2. 默认情况下,将其插入所选节点之后更为合理。
  3. 您将D的下一个节点设置为Z,E的上一个节点设置为Z,Z的下一个节点设置为E,Z的上一个节点设置为D。

希望我能正确理解您的问题,这有点令人困惑。

使用JAVA的双链表实现。 已实施的操作有:

  1. 在DLL中插入节点。
  2. 从DLL中删除第n个position元素。
  3. 查找元素在DLL中的位置。
  4. 从DLL中检索所有节点。
  5. 从DLL反向检索所有节点。
  6. 获取DLL的长度。

     package com.config; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class DLL { public static void main(String args[]) throws NumberFormatException, IOException{ int choice = 0; Node temp = null; Node head = null; boolean flage = true; int nodeCounter = 0; BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); do{ Node node = new Node(); System.out.println("Enter Node data"); node.data = Integer.parseInt(read.readLine()); if(flage){ flage = false; head = node; } if(temp!=null){ temp.next = node; node.prev = temp; } temp = node; nodeCounter++; System.out.println("Press 0 to quite for more node entry."); choice = Integer.parseInt(read.readLine()); }while(choice != 0); temp.next = head; head.prev = temp; System.out.println("You have created "+nodeCounter+" nodes in doubly linked list"); System.out.println("Retriving and Manipulation Operation perform : "); Node node = head; do{ System.out.println("Press 1 for get all nodes from doubly linked list."); System.out.println("Press 2 for get all nodes in reverse from doubly linked list."); System.out.println("Press 3 for get length of doubly linked list."); System.out.println("Press 4 for remove nth node from doubly linked list."); System.out.println("Press 5 for find node in doubly linked list."); System.out.println("Press 0 for quite."); choice = Integer.parseInt(read.readLine()); switch (choice) { case 1: Node.getAllNodes(node); break; case 2: Node.getAllNodesReverse(node); break; case 3 : System.out.println("Length : "+Node.getDoublyLinkedListLength(node)); break; case 4 : System.out.println("Enter Position to remove from DLL"); choice = Integer.parseInt(read.readLine()); node = Node.removeNthNode(node, choice); break; case 5 :System.out.println("Enter Node data to find in DLL"); choice = Integer.parseInt(read.readLine()); choice = Node.findNode(node, choice); if(choice == 0){ System.out.println("Node Not Found in DLL"); choice = 1; }else System.out.println("Node Position : "+choice); break; default: break; } }while(choice != 0); } } class Node{ int data = 0; Node next = null; Node prev = null; public static void getAllNodes(Node head){ int nodeCounter = 0; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; System.out.println(nodeCounter+". Node : "+head.data); head = head.next; } nodeCounter++; System.out.println(nodeCounter+". Node : "+head.data); } } public static int getDoublyLinkedListLength(Node head){ int nodeCounter = 0; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; head = head.next; } nodeCounter++; } return nodeCounter; } public static int findNode(Node head,int data){ int nodeCounter = 0; boolean flage = false; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; if(head.data == data){ flage = true; break; } head = head.next; } } return flage ? nodeCounter : 0; } public static void getAllNodesReverse(Node head){ if(head!= null){ int nodeCounter = 0; Node tail = head.prev; while(tail.prev!= head.prev){ nodeCounter++; System.out.println(nodeCounter+". Node : "+tail.data); tail = tail.prev; } nodeCounter++; System.out.println(nodeCounter+". Node : "+tail.data); } } public static Node removeNthNode(Node head, int removePosition){ int length = getDoublyLinkedListLength(head); if(head!=null){ int counter = 1; if(length >2){ if(length+1 > removePosition && removePosition > 0){ while(counter != removePosition){ counter++; head = head.next; } head.prev.next = head.next; head.next.prev = head.prev; }else{ System.out.println("Given Position not exist"); } }else{ System.out.println("At least two nodes must be in doubly linked list."); } } if(removePosition==1 || removePosition==length) return head.next; else return head; } } 

暂无
暂无

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

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