Inserting a Node Into a Sorted Doubly Linked List

Sort by

recency

|

1073 Discussions

|

  • + 0 comments

    Ensure proper traversal to find the correct position, updating both forward and backward links. Maintain sorted order by adjusting pointers carefully to prevent breaking the list structure. Ekbet16 Com Login

  • + 0 comments

    in C:

    DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* llist, int data) {
        DoublyLinkedListNode** cur = &llist;
        DoublyLinkedListNode * new_node = calloc(sizeof(DoublyLinkedListNode), 1);
        new_node->data = data;
        
        while(*cur && ((*cur)->data < data)) {
            cur = &(*cur)->next;
        }
        
        new_node->next = (*cur);
        if(*cur) {
            new_node->prev = (*cur)->prev;
        }
        (*cur) = new_node;
        return llist;
    }
    
  • + 0 comments

    Python 3 code

    Inserting a Node Into a Sorted Doubly Linked List

    def sortedInsert(llist, data):
        # q - new node 
        q = DoublyLinkedListNode(data)
        # p - pointer to linked list
        p = llist
        
        # llist is empty
        if(not p):
            return q
            
        # insert at the beginning    
        if(p and data < p.data):
            q.next = p
            p.prev = q
            p = q
            return p
        # while traversing pointer p   
        while(p):
            
            p = p.next
            
            # In-Between 2 nodes
            if(p.data > q.data):
                
                p = p.prev            
                q.next = p.next
                q.prev = p
                if(p.next):
                    p.next.prev = q
                p.next = q
                return llist
                
            else:
                # inserting at the end of the linked list
                if(not p.next):
                    
                    p.next = q
                    q.next = None
                    q.prev = p
                    return llist
                    
                else:
                    continue
       
    

    Do Upvote it if you found it helpful & Do Comment below for any suggestions⛳

  • + 0 comments

    Any c++ help?

  • + 0 comments
    *PYTHON Whenever we have to change the head ,its best practice to create a dummy node as head
    
    def sortedInsert(llist, data):
        dummy=DoublyLinkedListNode(float('-inf'))
        dummy.next=llist
        llist.prev=dummy
        y=dummy
        while (dummy):
            previous=dummy
            if(dummy.next and dummy.next.data>=data and dummy.data<=data):
                x=DoublyLinkedListNode(data)
                dummy.next,x.next=x,dummy.next
                x.prev=dummy
                x=x.next.prev
                return y.next
            dummy=dummy.next
                
                
          
        x=DoublyLinkedListNode(data)
        x.prev=previous
        previous.next=x
            
                
        return y.next