Home » Java » java – How to shift elements after adding in Doubly Linked List-Exceptionshub

java – How to shift elements after adding in Doubly Linked List-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have added an element to a specified position in a Doubly Linked List, but I want to shift all the elements that was at that position and after it to the right? Any tips on how to approach this problem?

public void addFirst(E e){
    DNode<E> headerNext = header.getNext();
    DNode<E> tempN = new DNode <E>(e, header, headerNext);
    headerNext.setPrev(tempN);
    header.setNext(tempN);
    size++;
}
public void add(int pos , E e ){
    DNode<E> ptr = new DNode<E> (e, null, null);
        if(pos == 1){
            addFirst(e);
            return;
        }
    DNode<E> optr = header;
    for (int i = 2; i <= size; i++) {
        if (i == pos) {
            DNode<E> tempN = optr.getNext();
            ptr.setNext(ptr);
            ptr.setPrev(optr);
            ptr.setPrev(tempN);
            tempN.setPrev(ptr);
        }
        ptr = ptr.getNext();
    }
    size++ ;
}
    }
How to&Answers:

Based example in your comment:

  1. You need to update the next pointer of new node to point to element 3’s address
  2. Element 3’s previous pointer to new node address
  3. New node previous address with element 3’s previous address
  4. Lastly Goto element 3’s previous address(node) and update its next pointer to new node address.

Answer:

Any tips on how to approach this problem?

Since you asked for tips on how to get started and you already have a doubly linked list here is a simple example.

Given the following is a Node in a Doubly Linked List.

// Doubly Linked list Node
class Node { 
    int data; 
    Node prev; 
    Node next; 

    // Constructor to create a new node 
    // next and prev is by default initialized as null 
    Node(int d) { data = d; } 
}

The following method can be implemented for your doubly linked list. You would provide a Node that would have a new Node inserted after the Node that you provide and the next and prev pointers for the current Node and the newly added Node would all be updated to reflect the newly added Node.

/* Given a node (currentnode). insert a new node after the given 
  node(currentNode) */
public void InsertAfterNode(Node currentNode, int newData) 
{ 

// check if the given current node is NULL 
if (currentNode == null) { 
    System.out.println("The given current node cannot be NULL "); 
    return; 
} 

//  allocate node  
//  put in the data 
Node new_node = new Node(newData); 

//  Make next of new node as next of current node
new_node.next = currentNode.next; 

//  Make the next of current node as new_node
currentNode.next = new_node; 

//  Make current node as previous of new_node 
new_node.prev = currentNode; 

//  Change previous of new_node's next node 
if (new_node.next != null) 
    new_node.next.prev = new_node; 
} 

You could implement a similar method, in reverse, to insert a new Node before the specific Node.