OneCompiler

SJ9923D4

125
  1. Write a JavaScript program to delete a node from doubly Linked List?
    Algorithm 
     Let the node to be deleted be del.
     If node to be deleted is head node, then change the head pointer to
    next current head.
    if headnode == del then
    headnode = del.nextNode
     Set prev of next to del, if next to del exists.
    if del.nextNode != none
    del.nextNode.previousNode = del.previousNode
     Set next of previous to del, if previous to del exists.
    if del.previousNode != none
    del.previousNode.nextNode = del.next
<script>   var head;       class Node     {                constructor(val)                           {                      this.data = val;                          this.prev = null;                          this.next = null;         }     }       function push(new_data)          {          new_Node = new Node(new_data);                     new_Node.next = head;         new_Node.prev = null;         if (head != null)             head.prev = new_Node;           head = new_Node;     }       function printlist( node) {          last = null;           while (node != null) {             document.write(node.data +"");             last = node;             node = node.next;         }           document.write("<br/>");     }            function deleteNode( del) {           if (head == null || del == null) {             return;         }           if (head == del) {             head = del.next;         }           if (del.next != null) {             del.next.prev = del.prev;         }                    if (del.prev != null) {             del.prev.next = del.next;         }           return;     }               push(2);           push(4);           push(8);           push(10);           document.write("Created DLL is: ");         printlist(head);         deleteNode(head);         deleteNode(head.next);         deleteNode(head.next);           document.write("Modified Linked list: ");         printlist(head);     </script>