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
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...
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?
After learning about Doubly Linked Lists, Come up with at least 2 reasons why you would...
After learning about Doubly Linked Lists, Come up with at least 2 reasons why you would use a Doubly Linked List instead of a regular (single) Linked List and then come up with at least 2 reasons why you would use a single Linked List instead of Doubly Linked List.
In C++ Create a program that uses only Selection Sort and Insertion Sort for the National...
In C++ Create a program that uses only Selection Sort and Insertion Sort for the National Football League list of current players Inputting data from a text file named "NFLplayers.txt" Only want the name of the player(first name then last name), their team name, and position they play. Example is: Patrick Mahomes, Chiefs, Quarterback Output lists to a text file along with how long it took to go through the Selection Sort and Insertion Sort and how many iterations it...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT