answersLogoWhite

0

The implementation of a sorted doubly linked list is similar to an unsorted doubly linked list. The only change will be in the insertion function where you will need to search for the position where the new element should be inserted.

Consider a doubly linked list with node structure as:


typedef struct node{

int data;

node *next. *prev;

}node;


node *head = NULL;


int isempty(){//This function returns whether the list is empty or not

return !head;

}


The code for insertion:


void insert( int num, int ** head){

node *temp, *temp1= *head;

temp = (node *)malloc(sizeof(node));

if( temp == NULL){

printf("Not enough space\n");

return;

}

temp->data = num;

temp->next = temp->prev = NULL;

if(isempty()){//insertion when the list is empty

*head= temp;

return;

}

if( temp1->data > num){//Insertion at the head

temp->next = head;

temp1->prev = num;

return;

}

while( temp1->next != NULL && temp->next->data < num)//Determiningthe postion of the element for insertion

temp1 = temp1->next;

temp->next = temp1->next;

if( !temp->next)

temp->next->prev = temp;

temp->prev = temp1;

temp1->next = temp;

}


If you want to know how to implement a normal doubly linked list, check the related liks below.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What advantages of a sorted list over a linked list?

All lists are linked lists; there is no such thing as a separate "sorted list". There are algorithms that can sort a list, of course, but they all work on linked lists.


How can I implement a merge sort algorithm for a doubly linked list in Java?

To implement a merge sort algorithm for a doubly linked list in Java, you can follow these steps: Divide the doubly linked list into two halves. Recursively sort each half using merge sort. Merge the two sorted halves back together in sorted order. You can achieve this by creating a mergeSort() method that takes the doubly linked list as input and recursively divides and merges the list. Make sure to handle the merging process for doubly linked lists by adjusting the pointers accordingly. Here is a basic outline of how you can implement this algorithm in Java: java public class MergeSortDoublyLinkedList public Node mergeSort(Node head) if (head null head.next null) return head; Node middle getMiddle(head); Node nextOfMiddle middle.next; middle.next null; Node left mergeSort(head); Node right mergeSort(nextOfMiddle); return merge(left, right); private Node merge(Node left, Node right) if (left null) return right; if (right null) return left; Node result null; if (left.data right.data) result left; result.next merge(left.next, right); result.next.prev result; else result right; result.next merge(left, right.next); result.next.prev result; return result; private Node getMiddle(Node head) if (head null) return head; Node slow head; Node fast head; while (fast.next ! null fast.next.next ! null) slow slow.next; fast fast.next.next; return slow; class Node int data; Node prev; Node next; public Node(int data) this.data data; This code snippet provides a basic implementation of the merge sort algorithm for a doubly linked list in Java. You can further customize and optimize it based on your specific requirements.


Which is preferred for writing a Merge Sort routine contiguous or linked memory?

Linked memory because its very useful primarily when the lists to be sorted are very large and the size of data to be moved is small.


What are the disadvantages of Fibonacci search?

There are a few disadvantages of the Fibonacci search: It can be slower than other search algorithms if the data is not sorted. It can be less accurate than other search algorithms if the data is not sorted. It can be more difficult to implement than other search algorithms.


Into what 3 categories is paper sorted?

where and how is paper sorted? where and how is paper sorted?


How do you get sorted on harrypottercom?

You cannot get sorted on that website. However, you can be sorted on Pottermore.


How do poorly sorted and well sorted sediments differ?

the well sorted is on the left & the poorly sorted is on the right .. It all depends on the arrangement and size of the rocks


How can data be sorted in a database?

it can be sorted by files kamran :)


Do kettle lakes contain sorted or unsorted sediments?

sorted


Is duplicated data is accepted in linked list?

Yes definitely, a linked list can accept duplicate data. As the data of each node does not have any concern with data of other node. The node differs from each other in their addresses. Until user does not make the linked list to accept unique data, the linked list can accept duplicates. if unsorted (e.g. representig a queue): yes if sorted (e.g. representing a set): should be decided design-time


How many minimum no of queues Rae needed to implement priority queue?

Two possible solutions: 1. Separated queue for every possible priority value. 2. One shared queue for every elements, sorted by priority.


What is the past participle of sort?

"Sorted" is the past participle of "sort".