Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Implement Sorted Doubly Linked List in C++



Sorted Doubly Linked List

A sorted doubly linked list is a type of doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. Where, insertion operation makes sure that the new node is placed in its correct sorted position.

A doubly linked list is a two-way linked list in which a node is connected with two pointers, i.e., next and previous pointers, which are references to the next node and previous node, respectively.

sorted-doubly-linkedList

Characteristics of Sorted Doubly Linked List

The following are the characteristics of the doubly linked list:
  • Sorted: All the inserted node will be in sorted order either in ascending or descending order.
  • Doubly Linked: Each node in a circular linked list has two pointers. (i.e., next and previous).
  • Header Node: A doubly linked list has a header node, which is frequently used to make execution of a certain operation.

Pseudocode of Sorted Doubly Linked List

Following is the step to implement the Sorted doubly linked list:

Begin
   function createnode() to insert node in the list:
      it creates a newnode and inserts the number in the data field of the newnode.
      It checks whether the list is empty or not. If the list is empty put the node as first element and update head.
      Both prev and next pointers will be initialized with NULL.
      If list is not empty, then this newnode will be inserted into the existing linked list in such a way that ultimately the linked list will remain sorted.
      Prev and next pointers will be updated accordingly.
End
Begin
   function display_head() to display the list from the head:
      Initialize c=0.
      Initialize pointer variable with the head node address
      while (c <= i )
         Print the node info
         Update pointer variable
         Increment c.
End
Begin
   function dispslay_tail() to display the list from the tail:
      Initialize m=0.
      Initialize pointer variable with the tail node address
      while (m <= i )
         Print the node info
         Update pointer variable
         Increment m.
End

Program to Implement Sorted Doubly Linked List in C++

In the following example, we implement a sorted doubly linked list in C++:

#include<iostream>

using namespace std;
struct nod {
   int d;
   nod * n, * p;
}

* p = NULL, * head = NULL, * r = NULL, * np = NULL, * tail = NULL;
int c = 0;

void createnode(int n) {
   np = new nod;
   np -> d = n;
   np -> n = NULL;
   np -> p = NULL;
   if (c == 0) {
      tail = np;
      head = np;
      p = head;
      p -> n = head;
      p -> p = head;
      c++;
   } else if (c == 1) {
      p = head;
      r = p;
      if (np -> d < p -> d) {
         np -> n = p;
         p -> p = np;
         head = np;
         p -> n = np;
         np -> p = p;
         tail = p;
      } else if (np -> d > p -> d) {
         p -> n = np;
         np -> p = p;
         np -> n = head;
         p -> p = np;
      }
      c++;
   } else {
      p = head;
      r = p;
      if (np -> d < p -> d) {
         np -> n = p;
         p -> p = np;
         head = np;
         do {
            p = p -> n;
         }
         while (p -> n != r);
         tail = p;
         p -> n = np;
         np -> p = p;
      } else if (np -> d > p -> d) {
         while (p -> n != head && np -> d > p -> d) {
            r = p;
            p = p -> n;
            if (p -> n == head && (p -> d < np -> d)) {
               p -> n = np;
               np -> p = p;
               np -> n = head;
               tail = np;
               head -> p = np;
               break;
            } else if (np -> d < p -> d) {
               r -> n = np;
               np -> p = r;
               np -> n = p;
               p -> p = np;
               if (p -> n != head) {
                  do {
                     p = p -> n;
                  }
                  while (p -> n != head);
               }
               tail = p;
               break;
            }
         }
      }
   }
}
void display_head(int i) {
   nod * t = head;
   int c = 0;
   while (c < i) {
      cout << t -> d << "\t";
      t = t -> n;
      c++;
   }
   cout << endl;
}

void display_tail(int i) {
   nod * t = tail;
   int m = 0;
   while (m < i) {
      cout << t -> d << "\t";
      t = t -> p;
      m++;
   }
   cout << endl;
}

int main() {
   int predefined_values[] = { 5, 3, 6, 9, 4 };
   int n = sizeof(predefined_values) / sizeof(predefined_values[0]);

   for (int i = 0; i < n; i++) {
      createnode(predefined_values[i]);
   }

   cout << "\nsorting Doubly Linked List head first\n";
   display_head(n);

   cout << "\nsorting Doubly Linked List tail first\n";
   display_tail(n);
}

Output of the sorted doubly linked list:

sorting Doubly Linked List head first
3	4	5	6	9	

sorting Doubly Linked List tail first
9	6	5	4	3	
Updated on: 2025-05-22T18:12:31+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements