簡體   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