Bubble Sort On Doubly Linked List | structure of a node ; Function to insert a node at the beginning of a linked list ; Function to print nodes in a given linked list ; Bubble sort the given linked list ; Checking for empty list ; Driver code ; start with empty linked list ; Create linked list from the array arr [ ] . Created linked list will be 1 -> 11 -> 2 -> 56 -> 12 ; print list before sorting ; sort the linked list ; print list after sorting
class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } function insertAtTheBegin ( start_ref , data ) { var ptr1 = new Node ( ) ; ptr1 . data = data ; ptr1 . next = start_ref ; if ( start_ref != null ) start_ref . prev = ptr1 ; start_ref = ptr1 ; return start_ref ; } function printList ( start ) { var temp = start ; document . write ( " " ) ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } function bubbleSort ( start ) { var swapped ; var ptr1 ; var lptr = null ; if ( start == null ) return null ; do { swapped = 0 ; ptr1 = start ; while ( ptr1 . next != lptr ) { if ( ptr1 . data > ptr1 . next . data ) { var t = ptr1 . data ; ptr1 . data = ptr1 . next . data ; ptr1 . next . data = t ; swapped = 1 ; } ptr1 = ptr1 . next ; } lptr = ptr1 ; } while ( swapped != 0 ) ; return start ; } var arr = [ 12 , 56 , 2 , 11 , 1 , 90 ] ; var i ; var start = null ; for ( i = 0 ; i < 6 ; i ++ ) start = insertAtTheBegin ( start , arr [ i ] ) ; document . write ( " " ) ; printList ( start ) ; start = bubbleSort ( start ) ; document . write ( " " ) ; printList ( start ) ;