Skip to main content

Sorted order insertion in doubly linked list in c

C program for Sorted order insertion in doubly linked list. Here more information.

// Include header file
#include <stdio.h>
#include <stdlib.h>
// C Program For
// Insert node in sorted order 

// Define structure of linked list Node
typedef struct LinkNode
{
	// Define useful field of LinkNode
	int data;
	struct LinkNode * next;
	struct LinkNode * prev;
}LinkNode;

LinkNode * getLinkNode(int data)
{
	// Create dynamic memory of LinkNode
	LinkNode * me = (LinkNode * ) malloc(sizeof(LinkNode));
	if (me == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
    // Set node value
	me->data = data;
	me->next = NULL;
	me->prev = NULL;
	return me;
}
typedef struct DoublyLinkedList
{
	// Define useful field of DoublyLinkedList
	struct LinkNode * head;
}DoublyLinkedList;

DoublyLinkedList * getDoublyLinkedList()
{
	// Create dynamic memory of DoublyLinkedList
	DoublyLinkedList * me = 
      (DoublyLinkedList * ) malloc(sizeof(DoublyLinkedList));
	if (me == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Set inital value
	me->head = NULL;
	return me;
}
// Insert Node at sorted way
void insert(DoublyLinkedList * me, int value)
{
	// Create a dynamic node
	LinkNode * node = getLinkNode(value);
	if ((me->head == NULL))
	{
		// Add first node
		me->head = node;
	}
	else if ((me->head->data >= node->data))
	{
		// When need to add node at begining position
		me->head->prev = node;
		node->next = me->head;
		me->head = node;
	}
	else
	{
		LinkNode * current = me->head;
		// Find position to add new node
		while (current != NULL && 
               current->next != NULL && 
               current->next->data <= value)
		{
			current = current->next;
		}
		// Insert new node between the nodes
		node->next = current->next;
		if ((current->next != NULL))
		{
			// When next node exists
			current->next->prev = node;
		}
		node->prev = current;
		current->next = node;
	}
}
// Display node element of doubly linked list
void display(DoublyLinkedList * me)
{
	if ((me->head == NULL))
	{
		printf("Empty Linked List");
	}
	else
	{
		printf("Linked List Head to Tail : ");
		// Get first node of linked list
		LinkNode * temp = me->head;
		LinkNode * last = NULL;
		// iterate linked list 
		while (temp != NULL)
		{
			// Display node value
			printf("%d  ", temp->data);
			last = temp;
			// Visit to next node
			temp = temp->next;
		}
		printf("\nLinked List Tail to Head : ");
		// Get last node of linked list
		temp = last;
		// iterate linked list 
		while (temp != NULL)
		{
			// Display node value
			printf("%d  ", temp->data);
			// Visit to prev node
			temp = temp->prev;
		}
	}
}
int main()
{
	DoublyLinkedList * dll = getDoublyLinkedList();
	// Insert following linked list nodes
	insert(dll, 5);
	insert(dll, 3);
	insert(dll, 11);
	insert(dll, 4);
	insert(dll, 6);
	insert(dll, 1);
	insert(dll, 8);
	insert(dll, 12);
	display(dll);
}

Output

Linked List Head to Tail : 1  3  4  5  6  8  11  12
Linked List Tail to Head : 12  11  8  6  5  4  3  1




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