Javatpoint Logo
Javatpoint Logo

Q. Program to sort the elements of the doubly linked list.

Explanation

In this program, we will create a doubly linked list and sort nodes of the list in ascending order.

  • Original List:
  • Program to sort the elements of the doubly linked list

    Sorted List:

    Program to sort the elements of the doubly linked list

    To accomplish this, we maintain two pointers: current and index. Initially, current point to head node and index will point to node next to current. Traverse through the list till current points to null, by comparing current's data with index's data. If the current's data is greater than the index's data, then swap data between them. In above example, current will initially point to 7, and the index will point to 1. Since, 7 is greater than 1, swap the data. Continue this process till the entire list is sorted in ascending order.

    Algorithm

    1. Define a Node class which represents a node in the list. It will have three properties: data, previous which will point to the previous node and next which will point to the next node.
    2. Define another class for creating a doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
    3. addNode() will add node to the list:
      1. It first checks whether the head is null, then it will insert the node as the head.
      2. Both head and tail will point to a newly added node.
      3. Head's previous pointer will point to null and tail's next pointer will point to null.
      4. If the head is not null, the new node will be inserted at the end of the list such that new node's previous pointer will point to tail.
      5. The new node will become the new tail. Tail's next pointer will point to null.
    4. sortList() will sort nodes of the list in ascending order.
      1. Define a node current which will point to head.
      2. Define another node index which will point to node next to current.
      3. Compare data of current and index node. If the current's data is greater than the index's data, then swap the data between them.
      4. Current will point to current.next and index will point to index.next.
      5. Continue this process till the entire list is sorted.
    5. display() will show all the nodes present in the list.
      1. Define a new node 'current' that will point to the head.
      2. Print current.data till current points to null.
      3. Current will point to the next node in the list in each iteration.

    Solution

    Python

    Output:

    Original list: 
    7 1 4 5 2 
    Sorted list: 
    1 2 4 5 7 
    

    C

    Output:

    Original list: 
    7 1 4 5 2 
    Sorted list: 
    1 2 4 5 7 
    

    JAVA

    Output:

    Original list: 
    7 1 4 5 2 
    Sorted list: 
    1 2 4 5 7 
    

    C#

    Output:

    Original list: 
    7 1 4 5 2 
    Sorted list: 
    1 2 4 5 7 
    

    PHP

    Output:

    Original list: 
    7 1 4 5 2 
    Sorted list: 
    1 2 4 5 7 
    

    Next Topic#




    Please Share

    facebook twitter google plus pinterest

    Learn Latest Tutorials