📜  双链表的插入排序

📅  最后修改于: 2021-05-04 20:06:42             🧑  作者: Mango  1)创建一个空的排序(或结果)双向链表。
2)遍历给定的双向链表，对每个节点执行以下操作。
……a)在排序(或结果)双向链表中以排序方式插入当前节点。
3)将给定链接列表的标题更改为排序(或结果)列表的标题。

##### C++
``````// C++ implementation for insertion Sort
// on a doubly linked list
#include

using namespace std;

// Node of a doubly linked list
struct Node {
int data;
struct Node* prev, *next;
};

// function to create and return a new node
// of a doubly linked list
struct Node* getNode(int data)
{
// allocate node
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));

// put in the data
newNode->data = data;
newNode->prev = newNode->next = NULL;
return newNode;
}

// function to insert a new node in sorted way in
// a sorted doubly linked list
void sortedInsert(struct Node** head_ref, struct Node* newNode)
{
struct Node* current;

// if list is empty

// if the node is to be inserted at the beginning
// of the doubly linked list
else if ((*head_ref)->data >= newNode->data) {
newNode->next->prev = newNode;
}

else {

// locate the node after which the new node
// is to be inserted
while (current->next != NULL &&
current->next->data < newNode->data)
current = current->next;

newNode->next = current->next;

// if the new node is not inserted
// at the end of the list
if (current->next != NULL)
newNode->next->prev = newNode;

current->next = newNode;
newNode->prev = current;
}
}

// function to sort a doubly linked list using insertion sort
{
// Initialize 'sorted' - a sorted doubly linked list
struct Node* sorted = NULL;

// Traverse the given doubly linked list and
// insert every node to 'sorted'
while (current != NULL) {

// Store next for next iteration
struct Node* next = current->next;

// removing all the links so as to create 'current'
// as a new node for insertion
current->prev = current->next = NULL;

// insert current in 'sorted' doubly linked list
sortedInsert(&sorted, current);

// Update current
current = next;
}

}

// function to print the doubly linked list
{
cout << head->data << " ";
}
}

// function to insert a node at the beginning of
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));

/* put in the data  */
new_node->data = new_data;

/* Make next of new node as head and previous as NULL */
new_node->prev = NULL;

/* change prev of head node to new node */

/* move the head to point to the new node */
}

// Driver program to test above
int main()
{

// insert the following data

cout << "Doubly Linked List Before Sortingn";

cout << "nDoubly Linked List After Sortingn";

return 0;
}``````

##### Java
``````// Java implementation for insertion Sort
// on a doubly linked list
class Solution
{

// Node of a doubly linked list
static class Node
{
int data;
Node prev, next;
};

// function to create and return a new node
// of a doubly linked list
static Node getNode(int data)
{
// allocate node
Node newNode = new Node();

// put in the data
newNode.data = data;
newNode.prev = newNode.next = null;
return newNode;
}

// function to insert a new node in sorted way in
// a sorted doubly linked list
static Node sortedInsert(Node head_ref, Node newNode)
{
Node current;

// if list is empty

// if the node is to be inserted at the beginning
// of the doubly linked list
{
newNode.next.prev = newNode;
}

else
{

// locate the node after which the new node
// is to be inserted
while (current.next != null &&
current.next.data < newNode.data)
current = current.next;

newNode.next = current.next;

// if the new node is not inserted
// at the end of the list
if (current.next != null)
newNode.next.prev = newNode;

current.next = newNode;
newNode.prev = current;
}
}

// function to sort a doubly linked list using insertion sort
{
// Initialize 'sorted' - a sorted doubly linked list
Node sorted = null;

// Traverse the given doubly linked list and
// insert every node to 'sorted'
while (current != null)
{

// Store next for next iteration
Node next = current.next;

// removing all the links so as to create 'current'
// as a new node for insertion
current.prev = current.next = null;

// insert current in 'sorted' doubly linked list
sorted=sortedInsert(sorted, current);

// Update current
current = next;
}

}

// function to print the doubly linked list
{
{
}
}

// function to insert a node at the beginning of
static Node push(Node head_ref, int new_data)
{
// allocate node /
Node new_node = new Node();

// put in the data /
new_node.data = new_data;

// Make next of new node as head and previous as null /
new_node.prev = null;

// change prev of head node to new node /

// move the head to point to the new node /

}

// Driver code
public static void main(String args[])
{

// insert the following data

System.out.println( "Doubly Linked List Before Sorting\n");

}
}

// This code is contributed by Arnab Kundu``````

##### Python3
``````# Python3 implementation for insertion Sort
# on a doubly linked list

# Node of a doubly linked list
class Node:

def __init__(self, data):
self.data = data
self.prev = None
self.next = None

# function to create and return a new node
# of a doubly linked list
def getNode(data):

# allocate node
newNode = Node(0)

# put in the data
newNode.data = data
newNode.prev = newNode.next = None
return newNode

# function to insert a new node in sorted way in
# a sorted doubly linked list

current = None

# if list is empty

# if the node is to be inserted at the beginning
# of the doubly linked list
newNode.next.prev = newNode

else :

# locate the node after which the new node
# is to be inserted
while (current.next != None and
current.next.data < newNode.data):
current = current.next

newNode.next = current.next

# if the new node is not inserted
# at the end of the list
if (current.next != None):
newNode.next.prev = newNode

current.next = newNode
newNode.prev = current

# function to sort a doubly linked list
# using insertion sort

# Initialize 'sorted' - a sorted
sorted = None

# Traverse the given doubly linked list
# and insert every node to 'sorted'
while (current != None) :

# Store next for next iteration
next = current.next

# removing all the links so as to create
# 'current' as a new node for insertion
current.prev = current.next = None

# insert current in 'sorted' doubly linked list
sorted = sortedInsert(sorted, current)

# Update current
current = next

# Update head_ref to point to

# function to print the doubly linked list

print( head.data, end = " ")

# function to insert a node at the
# beginning of the doubly linked list

""" allocate node """
new_node = Node(0)

""" put in the data """
new_node.data = new_data

""" Make next of new node as head
and previous as None """
new_node.prev = None

""" change prev of head node to new node """

""" move the head to point to the new node """

# Driver Code
if __name__ == "__main__":

# insert the following data

print( "Doubly Linked List Before Sorting")

# This code is contributed by Arnab Kundu``````

##### C#
``````// C# implementation for insertion Sort
// on a doubly linked list
using System;

class GFG
{

// Node of a doubly linked list
public class Node
{
public int data;
public Node prev, next;
};

// function to create and return a new node
// of a doubly linked list
static Node getNode(int data)
{
// allocate node
Node newNode = new Node();

// put in the data
newNode.data = data;
newNode.prev = newNode.next = null;
return newNode;
}

// function to insert a new node in sorted way
// in a sorted doubly linked list
Node newNode)
{
Node current;

// if list is empty

// if the node is to be inserted at the beginning
// of the doubly linked list
{
newNode.next.prev = newNode;
}

else
{

// locate the node after which
// the new node is to be inserted
while (current.next != null &&
current.next.data < newNode.data)
current = current.next;

newNode.next = current.next;

// if the new node is not inserted
// at the end of the list
if (current.next != null)
newNode.next.prev = newNode;

current.next = newNode;
newNode.prev = current;
}
}

// function to sort a doubly linked list
// using insertion sort
{
// Initialize 'sorted' - a sorted doubly linked list
Node sorted = null;

// Traverse the given doubly linked list and
// insert every node to 'sorted'
while (current != null)
{

// Store next for next iteration
Node next = current.next;

// removing all the links so as to create
// 'current' as a new node for insertion
current.prev = current.next = null;

// insert current in 'sorted' doubly linked list
sorted = sortedInsert(sorted, current);

// Update current
current = next;
}

// Update head_ref to point to

}

// function to print the doubly linked list
{
{
}
}

// function to insert a node at the beginning of
static Node push(Node head_ref, int new_data)
{
// allocate node
Node new_node = new Node();

// put in the data
new_node.data = new_data;

// Make next of new node as head
// and previous as null
new_node.prev = null;

// change prev of head node to new node

// move the head to point to the new node

}

// Driver code
public static void Main(String []args)
{

// insert the following data

}
}

// This code is contributed by Princi Singh``````

``````Doubly Linked List Before Sorting
8 12 10 5 3 9