Skip to main content

Insertion sort on doubly linked list in c++

C++ program for Insertion sort on doubly linked list. Here problem description and other solutions.

// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Perform insertion sort on doubly linked list
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode *prev;
	LinkNode(int data)
	{
		this->data = data;
		this->next = nullptr;
		this->prev = nullptr;
	}
};
class DoublyLinkedList
{
	public: 
    LinkNode *head;
	LinkNode *tail;
	DoublyLinkedList()
	{
		this->head = nullptr;
		this->tail = nullptr;
	}
	// Insert new node at end position
	void insert(int value)
	{
		// Create a node
		LinkNode *node = new LinkNode(value);
		if (this->head == nullptr)
		{
			// Add first node
			this->head = node;
			this->tail = node;
			return;
		}
		// Add node at last position
		this->tail->next = node;
		node->prev = this->tail;
		this->tail = node;
	}
	// Display node element of doubly linked list
	void display()
	{
		if (this->head == nullptr)
		{
			cout << "Empty Linked List" << endl;
		}
		else
		{
			cout << "\nLinked List Head to Tail :";
			// Get first node of linked list
			LinkNode *temp = this->head;
			// iterate linked list 
			while (temp != nullptr)
			{
				// Display node value
				cout << "  " << temp->data;
				// Visit to next node
				temp = temp->next;
			}
			cout << "\nLinked List Tail to Head :";
			// Get last node of linked list
			temp = this->tail;
			// iterate linked list 
			while (temp != nullptr)
			{
				// Display node value
				cout << "  " << temp->data;
				// Visit to prev node
				temp = temp->prev;
			}
			cout << "\n";
		}
	}
	// Swap value of given node
	void swapData(LinkNode *first, LinkNode *second)
	{
		int value = first->data;
		first->data = second->data;
		second->data = value;
	}
	// Sort elements using insertion sort
	void insertionSort()
	{
		// Get first node
		LinkNode *front = this->head;
		LinkNode *back = nullptr;
		while (front != nullptr)
		{
			// Get next node
			back = front->next;
			// Update node value when consecutive nodes are not sort
			while (back != nullptr && 
                   back->prev != nullptr && 
                   back->data < back->prev->data)
			{
				// Modified node data
				this->swapData(back, back->prev);
				// Visit to previous node
				back = back->prev;
			}
			// Visit to next node
			front = front->next;
		}
	}
};
int main()
{
	DoublyLinkedList *dll = new DoublyLinkedList();
	//Insert element of linked list
	dll->insert(25);
	dll->insert(2);
	dll->insert(6);
	dll->insert(14);
	dll->insert(12);
	dll->insert(9);
	dll->insert(16);
	dll->insert(3);
	cout << "\n Before Sort";
	dll->display();
	// Display all node
	dll->insertionSort();
	cout << "\n After Sort";
	dll->display();
	return 0;
}

Output

 Before Sort
Linked List Head to Tail :  25  2  6  14  12  9  16  3
Linked List Tail to Head :  3  16  9  12  14  6  2  25

 After Sort
Linked List Head to Tail :  2  3  6  9  12  14  16  25
Linked List Tail to Head :  25  16  14  12  9  6  3  2




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment