简体   繁体   中英

Doubly Linked Lists Implementation

I have looked at most threads here about Doubly linked lists but still unclear about the following.

I am practicing the Goodrich and Tamassia book in Java. About doubly linked lists, please correct me if I am wrong, it is different from a singly linked list in that a node could be inserted anywhere, not just after the head or after the tail using both the next and prev nodes available, while in singly linked lists, this insertion anywhere in the list is not possible?

If one wants to insert a node in a doubly linked list, then the default argument should be either the node after the to-be inserted node or node before the to-be inserted node ? But if this is so, then I don't understand how to pass the node before or after. Should we be displaying all nodes that were inserted till now and ask the user to select the node before or after which some new node is to be inserted ? My doubt is how to pass this default node. Because I assume that will require the next and prev nodes of these nodes as well.

For eg, Head<->A<->B<->C<->D<->E<->tail

If Z is the new node to be inserted after say D, then how should node D be passed? I am confused with this though it seems pretty simple to most.

But pl do explain.

Thanks, Sanjay

Assuming a Node class like this:

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

Then you can define:

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

and

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

Hope that helps

it is different from a singly linked list in that a node could be inserted anywhere, not just after the head or after the tail using both the next and prev nodes available, while in singly linked lists, this insertion anywhere in the list is not possible?

This is wrong. You can insert nodes in the middle of a singly linked list just fine. The only difference is that a doubly linked list is easier to navigate, since you can walk it forward and backward from any given node.

If one wants to insert a node in a doubly linked list, then the default argument should be either the node after the to-be inserted node or node before the to-be inserted node?

That depends entirely on what you are using the list for. Linked lists are not really a good general-purpose data structure due to their poor random access performance. Because of that, they are usually only used for algorithms that iterate over the list anyway and insert or remove nodes while they do it.

But if this is so, then I don't understand how to pass the node before or after. Should we be displaying all nodes that were inserted till now and ask the user to select the node before or after which some new node is to be inserted?

You're approaching this from the wrong way, trying to write an application around a certain data structure. Normally you start out with the application's end-user requirements and then decide which data structure is best used to satisfy those requirements. A User should never have to understand low-level internals of a program like linked lists.

  1. You can insert in a single linked list too. A->B Becomes A->C->B, all you have to do is change A's next node reference and set C's to B.
  2. It makes more sense to insert after the selected node by default.
  3. You set D's next node to Z, E's previous node to Z, Z's next node to E and Z's previous node to D.

I hope I understood your questions correctly, they're a bit confusing.

Doubly Linked List Implementation using JAVA. Implemented Operations are :

  1. Insert node in DLL.
  2. Remove nth position element from DLL.
  3. Find position of element in DLL.
  4. Retrieve all nodes from DLL.
  5. Retrieve all nodes in reverse from DLL.
  6. Get length of 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; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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