Search an element in a Doubly Linked List | Structure of a node of the doubly linked list ; Stores data value of a node ; Stores pointer to next node ; Stores pointer to previous node ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate memory for new node ; Insert the data ; Since node is added at the beginning , prev is always null ; Link the old list to the new node ; If pointer to head is not null ; Change the prev of head node to new node ; Move the head to point to the new node ; Function to find the position of an integer in doubly linked list ; Stores head Node ; Stores position of the integer in the doubly linked list ; Traverse the doubly linked list ; Update pos ; Update temp ; If the integer not present in the doubly linked list ; If the integer present in the doubly linked list ; Driver Code ; Create the doubly linked list 18 < . 15 < . 8 < . 9 < . 14
class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } ; function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) { ( head_ref ) . prev = new_node ; } ( head_ref ) = new_node ; return head_ref ; } function search ( head_ref , x ) { var temp = head_ref ; var pos = 0 ; while ( temp . data != x && temp . next != null ) { pos ++ ; temp = temp . next ; } if ( temp . data != x ) return - 1 ; return ( pos + 1 ) ; } var head = null ; var X = 8 ; head = push ( head , 14 ) ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 15 ) ; head = push ( head , 18 ) ; document . write ( search ( head , X ) ) ;