insertion sort doubly linked list java
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 (head_ref == null)
head_ref = newNode;
// if the node is to be inserted at the beginning
// of the doubly linked list
else if ((head_ref).data >= newNode.data)
{
newNode.next = head_ref;
newNode.next.prev = newNode;
head_ref = newNode;
}
else
{
current = head_ref;
// locate the node after which the new node
// is to be inserted
while (current.next != null &&
current.next.data < newNode.data)
current = current.next;
//Make the appropriate links /
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;
}
return head_ref;
}
// function to sort a doubly linked list using insertion sort
static Node insertionSort(Node head_ref)
{
// Initialize 'sorted' - a sorted doubly linked list
Node sorted = null;
// Traverse the given doubly linked list and
// insert every node to 'sorted'
Node current = head_ref;
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 sorted doubly linked list
head_ref = sorted;
return head_ref;
}
// function to print the doubly linked list
static void printList(Node head)
{
while (head != null)
{
System.out.print(head.data + " ");
head = head.next;
}
}
// function to insert a node at the beginning of
// the doubly linked list
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.next = (head_ref);
new_node.prev = null;
// change prev of head node to new node /
if ((head_ref) != null)
(head_ref).prev = new_node;
// move the head to point to the new node /
(head_ref) = new_node;
return head_ref;
}
// Driver code
public static void main(String args[])
{
// start with the empty doubly linked list /
Node head = null;
// insert the following data
head=push(head, 9);
head=push(head, 3);
head=push(head, 5);
head=push(head, 10);
head=push(head, 12);
head=push(head, 8);
System.out.println( "Doubly Linked List Before Sorting\n");
printList(head);
head=insertionSort(head);
System.out.println("\nDoubly Linked List After Sorting\n");
printList(head);
}
}
// This code is contributed by Arnab Kundu
Also in Java:
- Title
- java difference ++i and i++ loop
- Category
- Java
- Title
- java int to int array
- Category
- Java
- Title
- spring-boot actuator not working
- Category
- Java
- Title
- How to make a class in Java?
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java
- Title
- spark write partitionby
- Category
- Java
- Title
- Java use Base64
- Category
- Java
- Title
- how to convert char to uppercase java
- Category
- Java
- Title
- how to do sex java
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- java connect to mysql
- Category
- Java
- Title
- Java sort Map by values
- Category
- Java
- Title
- java time code
- Category
- Java
- Title
- instantiate optinal java 8
- Category
- Java
- Title
- how to check the lines in a file java scanner
- Category
- Java
- Title
- java int to binary
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- how to print each element of an arraylist on a new line in java
- Category
- Java
- Title
- ARE THERE POINTER IN JAVA
- Category
- Java
- Title
- android create snackbar
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- java integer to binary string
- Category
- Java
- Title
- how to check if an arraylist contains a value in java recursion
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- android how to switch between activities
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- check if user has internet connection in kotlin
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- joptionpane fonctionnement java
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- java download file from url to string
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- break for loop java
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- binary number input in int java
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- java how to print an array
- Category
- Java
- Title
- Java create array of array
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- how to add multiple filter condition in Java stream filter chain
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- how to select a random element from an array in java
- Category
- Java
- Title
- Unhandled exception: java.lang.InterruptedException
- Category
- Java
- Title
- loop through array java
- Category
- Java
- Title
- java switch display panel
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- java int stream min
- Category
- Java
- Title
- localdate to string java
- Category
- Java
- Title
- java is power of 2
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- absolute value java
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- how to detect device javascirpt
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- JSONObject java
- Category
- Java
- Title
- java create txt file
- Category
- Java
- Title
- count word in string no matter the delimiter java
- Category
- Java
- Title
- java store hexadecimal value
- Category
- Java
- Title
- import
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- thread sleep java
- Category
- Java
- Title
- print hello world in java
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- java tester si un caractere est une lettre
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- prendere valore da tastiera java
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- java ternary operator
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- how to push an element in hashset java
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- how to compare current date and time with another date and time in android
- Category
- Java
- Title
- java Convert a string IPv4 IP address to the equivalent long numeric value.
- Category
- Java
- Title
- string startswith java
- Category
- Java
- Title
- array of objects in java
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the `--no-shrink` flag to this command.
- Category
- Java
- Title
- how to make int array java android
- Category
- Java
- Title
- abstract class java constructor
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- logger in java
- Category
- Java
- Title
- binary string to int java
- Category
- Java
- Title
- java string replace character at position
- Category
- Java
- Title
- iterate map in java
- Category
- Java
- Title
- calculating the percentile in java
- Category
- Java
- Title
- abstract class in java
- Category
- Java
- Title
- Java's BigInteger class
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- wraping list to string java
- Category
- Java
- Title
- guess the number java
- Category
- Java