Sorted insert in a doubly linked list with head and tail pointers

A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes.

The task is to create a doubly linked list by inserting nodes such that list remains in ascending order on printing from left to right. Also, we need to maintain two pointers, head (points to first node) and tail (points to last node).

Examples:

Input : 40 50 10 45 90 100 95
Output :10 40 45 50 90 95 100

Input : 30 10 50 43 56 12
Output :10 12 30 43 50 56

Algorithm:
The task can be accomplished as:

  1. If Linked list is empty then make both the left and right pointers point to the node to be inserted and make its previous and next field point to NULL.
  2. If node to be inserted has value less than the value of first node of linked list then connect that node from previous field of first node.
  3. If node to be inserted has value more than the value of last node of linked list then connect that node from next field of last node.
  4. If node to be inserted has value in between the value of first and last node, then check for appropriate position and make connections.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C++ program to insetail nodes in doubly 
linked list such that list remains in 
ascending order on printing from left 
to right */
#include <bits/stdc++.h>
using namespace std;
  
// A linked list node 
class Node 
    public:
    Node *prev; 
    int info; 
    Node *next; 
}; 
  
// Function to insetail new node 
void nodeInsetail(Node **head, 
                Node **tail, 
                int key) 
  
    Node *p = new Node(); 
    p->info = key; 
    p->next = NULL; 
  
    // If first node to be insetailed in doubly 
    // linked list 
    if ((*head) == NULL) 
    
        (*head) = p; 
        (*tail) = p; 
        (*head)->prev = NULL; 
        return
    
  
    // If node to be insetailed has value less 
    // than first node 
    if ((p->info) < ((*head)->info)) 
    
        p->prev = NULL; 
        (*head)->prev = p; 
        p->next = (*head); 
        (*head) = p; 
        return
    
  
    // If node to be insetailed has value more 
    // than last node 
    if ((p->info) > ((*tail)->info)) 
    
        p->prev = (*tail); 
        (*tail)->next = p; 
        (*tail) = p; 
        return
    
  
    // Find the node before which we need to 
    // insert p. 
    Node *temp = (*head)->next; 
    while ((temp->info) < (p->info)) 
        temp = temp->next; 
  
    // Insert new node before temp 
    (temp->prev)->next = p; 
    p->prev = temp->prev; 
    temp->prev = p; 
    p->next = temp; 
  
// Function to print nodes in from left to right 
void printList(Node *temp) 
    while (temp != NULL) 
    
        cout << temp->info << " "
        temp = temp->next; 
    
  
// Driver program to test above functions 
int main() 
    Node *left = NULL, *right = NULL; 
    nodeInsetail(&left, &right, 30); 
    nodeInsetail(&left, &right, 50); 
    nodeInsetail(&left, &right, 90); 
    nodeInsetail(&left, &right, 10); 
    nodeInsetail(&left, &right, 40); 
    nodeInsetail(&left, &right, 110); 
    nodeInsetail(&left, &right, 60); 
    nodeInsetail(&left, &right, 95); 
    nodeInsetail(&left, &right, 23); 
  
    cout<<"Doubly linked list on printing"
        " from left to right\n"
    printList(left); 
  
    return 0; 
  
// This is code is contributed by rathbhupendra

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C program to insetail nodes in doubly
linked list such that list remains in
ascending order on printing from left
to right */
#include<stdio.h>
#include<stdlib.h>
  
// A linked list node
struct Node
{
    struct Node *prev;
    int info;
    struct Node *next;
};
  
// Function to insetail new node
void nodeInsetail(struct Node **head,
                  struct Node **tail,
                  int key)
{
  
    struct Node *p = new Node;
    p->info = key;
    p->next = NULL;
  
    // If first node to be insetailed in doubly
    // linked list
    if ((*head) == NULL)
    {
        (*head) = p;
        (*tail) = p;
        (*head)->prev = NULL;
        return;
    }
  
    // If node to be insetailed has value less
    // than first node
    if ((p->info) < ((*head)->info))
    {
        p->prev = NULL;
        (*head)->prev = p;
        p->next = (*head);
        (*head) = p;
        return;
    }
  
    // If node to be insetailed has value more
    // than last node
    if ((p->info) > ((*tail)->info))
    {
        p->prev = (*tail);
        (*tail)->next = p;
        (*tail) = p;
        return;
    }
  
    // Find the node before which we need to
    // insert p.
    temp = (*head)->next;
    while ((temp->info) < (p->info))
        temp = temp->next;
  
    // Insert new node before temp
    (temp->prev)->next = p;
    p->prev = temp->prev;
    temp->prev = p;
    p->next = temp;
}
  
// Function to print nodes in from left to right
void printList(struct Node *temp)
{
    while (temp != NULL)
    {
        printf("%d ", temp->info);
        temp = temp->next;
    }
}
  
// Driver program to test above functions
int main()
{
    struct Node *left = NULL, *right = NULL;
    nodeInsetail(&left, &right, 30);
    nodeInsetail(&left, &right, 50);
    nodeInsetail(&left, &right, 90);
    nodeInsetail(&left, &right, 10);
    nodeInsetail(&left, &right, 40);
    nodeInsetail(&left, &right, 110);
    nodeInsetail(&left, &right, 60);
    nodeInsetail(&left, &right, 95);
    nodeInsetail(&left, &right, 23);
  
    printf("\nDoubly linked list on printing"
           " from left to right\n");
    printList(left);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java program to insetail nodes in doubly 
linked list such that list remains in 
ascending order on printing from left 
to right */
  
import java.io.*;
import java.util.*;
  
// A linked list node
class Node
{
    int info;
    Node prev, next;
}
  
class GFG
{
  
    static Node head, tail;
  
    // Function to insetail new node 
    static void nodeInsetail(int key)
    {
        Node p = new Node();
        p.info = key;
        p.next = null;
  
        // If first node to be insetailed in doubly 
        // linked list
        if (head == null)
        {
            head = p;
            tail = p;
            head.prev = null;
            return;
        }
  
        // If node to be insetailed has value less 
        // than first node 
        if (p.info < head.info)
        {
            p.prev = null;
            head.prev = p;
            p.next = head;
            head = p;
            return;
        }
              
        // If node to be insetailed has value more 
        // than last node 
        if (p.info > tail.info)
        {
            p.prev = tail;
            tail.next = p;
            tail = p;
            return;
        }
  
        // Find the node before which we need to 
        // insert p.
        Node temp = head.next;
        while (temp.info < p.info)
                temp = temp.next;
                  
        // Insert new node before temp 
        (temp.prev).next = p;
        p.prev = temp.prev;
        temp.prev = p;
        p.next = temp;
    }
  
    // Function to print nodes in from left to right
    static void printList(Node temp)
    {
        while (temp != null)
        {
                System.out.print(temp.info + " ");
                temp = temp.next;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        head = tail = null;
        nodeInsetail(30);
        nodeInsetail(50);
        nodeInsetail(90);
        nodeInsetail(10);
        nodeInsetail(40);
        nodeInsetail(110);
        nodeInsetail(60);
        nodeInsetail(95);
        nodeInsetail(23);
  
        System.out.println("Doubly linked list on printing from left to right");
        printList(head); 
    }
}
  
// This code is contributed by rachana soma

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C# program to insetail nodes in doubly 
linked list such that list remains in 
ascending order on printing from left 
to right */
using System; 
  
// A linked list node 
public class Node 
    public int info; 
    public Node prev, next; 
  
class GFG 
  
    static Node head, tail; 
  
    // Function to insetail new node 
    static void nodeInsetail(int key) 
    
        Node p = new Node(); 
        p.info = key; 
        p.next = null
  
        // If first node to be insetailed in doubly 
        // linked list 
        if (head == null
        
            head = p; 
            tail = p; 
            head.prev = null
            return
        
  
        // If node to be insetailed has value less 
        // than first node 
        if (p.info < head.info) 
        
            p.prev = null
            head.prev = p; 
            p.next = head; 
            head = p; 
            return
        
              
        // If node to be insetailed has value more 
        // than last node 
        if (p.info > tail.info) 
        
            p.prev = tail; 
            tail.next = p; 
            tail = p; 
            return
        
  
        // Find the node before which we need to 
        // insert p. 
        Node temp = head.next; 
        while (temp.info < p.info) 
            temp = temp.next; 
                  
        // Insert new node before temp 
        (temp.prev).next = p; 
        p.prev = temp.prev; 
        temp.prev = p; 
        p.next = temp; 
    
  
    // Function to print nodes in from left to right 
    static void printList(Node temp) 
    
        while (temp != null
        
            Console.Write(temp.info + " "); 
            temp = temp.next; 
        
    
  
    // Driver code 
    public static void Main(String []args) 
    
        head = tail = null
        nodeInsetail(30); 
        nodeInsetail(50); 
        nodeInsetail(90); 
        nodeInsetail(10); 
        nodeInsetail(40); 
        nodeInsetail(110); 
        nodeInsetail(60); 
        nodeInsetail(95); 
        nodeInsetail(23); 
  
        Console.WriteLine("Doubly linked list on printing from left to right"); 
        printList(head); 
    
  
// This code is contributed by Arnab Kundu

chevron_right


Output:

Doubly linked list on printing from left to right
10 23 30 40 50 60 90 95 110


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.