Question

How would I make a generic insertion sort for a doubly linked list in java? or...

How would I make a generic insertion sort for a doubly linked list in java?

or is it even possible to make it generic in the first place?

Homework Answers

Answer #1

CODE

static Node<T> sortedInsert(Node<T> head_ref, Node<T> newNode)

{

Node<T> current;

if (head_ref == null)

head_ref = newNode;

else if ((head_ref).data.compareTo(newNode.data) >= 0)

{

newNode.next = head_ref;

newNode.next.prev = newNode;

head_ref = newNode;

} else

{

current = head_ref;

while (current.next != null && current.next.datA.compareTo(newNode.data) < 0)

current = current.next;

newNode.next = current.next;

if (current.next != null)

newNode.next.prev = newNode;

current.next = newNode;

newNode.prev = current;

}

return head_ref;

}

static Node<T> insertionSort(Node<T> head_ref)

{

Node<T> sorted = null;

Node<T> current = head_ref;

while (current != null)

{

Node<T> next = current.next;

current.prev = current.next = null;

sorted = sortedInsert(sorted, current);

current = next;

}

head_ref = sorted;

return

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Generic Linked List Problem: How do I remove a slice of a linked list and...
Java Generic Linked List Problem: How do I remove a slice of a linked list and add its data to another linked list in java? Example: Private<T> head; Private int size; Public List<T> slice(int from, int to) { // This method will remove the data from the given range (inclusive) and add it to a new List and return this new list. }
How would I make a linked list of a linked list in C? For example I...
How would I make a linked list of a linked list in C? For example I want a linked list that holds all the words inputted from the user. Example: typedef struct node{ char *word; struct node *next; }node; This list will hold all the strings that were inputted from the user. What I want to do is have another linked list that holds all the words that are no vowel matches which means that they would be equal if...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
c++ code for function that take doubly linked list as parameter and deleted all nodes in...
c++ code for function that take doubly linked list as parameter and deleted all nodes in even position from first to last
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do this manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please do not copy from stackabuse or stackoverflow, Please explain that algorithm with comments. i really want to learn this concept.
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Write C language code for a function that takes a doubly linked list as a parameter...
Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.
a) Name three differences between vectors and linked list? b) Given a doubly linked list with...
a) Name three differences between vectors and linked list? b) Given a doubly linked list with five nodes, explain how can you remove the node in the middle and replace it with new node?
C++ Programming Question: Write a function that sorts a linked list. Use insertion, bubble, selection or...
C++ Programming Question: Write a function that sorts a linked list. Use insertion, bubble, selection or shell sort.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT