Question

Consider creating a method minToFront that moves the smallest element to the first node in the list of links in integer values. For example, if the variable list is storing a list of links such as [7, 9, 12, 5, 3, 17], the call to list.minToFront() would be the same as [3, 7, 9, 12, 5, 17]. If there is more than one minimum value, move the first one. If the list is empty, method calls have no effect. The LinkedIntList class is in the form below (using the ListNode class):

public class LinkedIntList t private ListNode front; public class ListNode public int data; public ListNode next

Complete the underlined parts of the code below.

public void minToFront() {

    if (front != null && front.next != null) {

        ListNode current = front;

        ListNode prev = front;

        while (current.next != null) {

            if (current.next.data < prev.next.data) {

                prev = current;

            }

            current = current.next;

        }

        if (prev.next != null && prev.next.data < front.data) {

            _____________________;

            prev.next = prev.next.next;

            min.next = front;

            front = min;

        }

    }

}

Please solve this problem. thank you:)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public void minToFront()

{

if (front!=null && frot.next!=null)

{

ListNode current=front;

ListNode prev=front;

while (current.next!=null)

{

if (current.next.data<prev.next.data)

{

prev=current;

}

current=current.next;

}

if (prev.next!=null && prev.next.data<front.data)

{

ListNode min=Prev.next;

prev.next=prev.next.next;

min.next=front;

front=min;

}

}

}

Add a comment
Know the answer?
Add Answer to:
Consider creating a method minToFront that moves the smallest element to the first node in the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help writing the helper function find() and removeAll(). I'm pretty sure i have find...

    I need help writing the helper function find() and removeAll(). I'm pretty sure i have find wrong. please help me! I included the top portion of my code so you get an idea of whats going going. on Java language someone asked structure of list node too? i dont know what they mean public class LinkedIntList public ListNode front; // first value in the list // post: constructs an empty list public LinkedIntList( front null; e public LinkedIntList(int[l arri this);...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • O indexOf Language/Type: Related Links: Author: Java implementing Linked Lists LinkedIntList LinkedIntList.java Marty Stepp Write a...

    O indexOf Language/Type: Related Links: Author: Java implementing Linked Lists LinkedIntList LinkedIntList.java Marty Stepp Write a method indexOf that accepts a value as a parameter and returns the index in the list of the first occurrence of that value, or -1 if the value is not found in the list. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list Type your solution...

  • Add a method to the DoubleLinkedList class built in class to reverse every set of values...

    Add a method to the DoubleLinkedList class built in class to reverse every set of values For example: 1, 2, 3, 4, 5, 6 Reverse 3: 3,2,1,6,5,4 Reverse 2: 2,1,4,3,6,5 Reverse 6: 6,5,4,3,2,1 Method header: public void reverseSegments(int setSize) outcome should be like this: Input:​​​​​​​​​​​​​​ 3 1 2 3 4 5 6 output: 3 2 1 6 5 4 Input:​​​​​​​​​​​​​​​​​​​​​ 2 ​​​​​​​1 2 3 4 5 6 output: 2 1 6 5 4 3 ============================================code====================================================================== public class MyDoubleLinkedList<E> { private...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • please explain how to code using java -. Implement the ListNode and LinkedIntList as we did...

    please explain how to code using java -. Implement the ListNode and LinkedIntList as we did in class (you can add other methods if needed). You can reference the coxle in powerpoint slides of LinkedList and your textbook. 2. Implement a LinkedIntList class with the following public operations. a. add(int value) - Adds the given value to the end of the list b.get(int index) - Retums value in list at given index. C.add( int index, int value) - Inserts the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT