Question

In: Computer Science

In JAVA Implement the moveMinToFront method in IntSinglyLinkedList. The moveMinToFront method looks through the list to...

In JAVA Implement the moveMinToFront method in IntSinglyLinkedList. The moveMinToFront method looks through the list to find the element with the minimum value. It moves that element to the front of the list.

Before abstract view: [7, 3, 2]

After Abstract view: [2,7,3]

Before Abstract view: [4,1,7]

After Abstract view: [1,4,7]

public void moveMinToFront() {

}

Test:

package net.datastructures;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Random;

public class IntSinglyLinkedListTest {
    @Test
    public void moveMinToFrontTestEmpty() {
        IntSinglyLinkedList s = new IntSinglyLinkedList();
        s.moveMinToFront();
        assertTrue(s.isEmpty());
    }

    @Test
    public void moveMinToFrontTest1() {
        IntSinglyLinkedList s = new IntSinglyLinkedList();
        s.addLast(10);
        s.addLast(20);
        s.addLast(30);
        s.addLast(40);
        s.addLast(50);
        s.moveMinToFront();
        assertEquals(10, (int)s.first());
        assertEquals(5, (int)s.size());
        assertEquals(50, (int)s.last());
    }

    @Test
    public void moveMinToFrontTest2() {
        IntSinglyLinkedList s = new IntSinglyLinkedList();
        s.addLast(20);
        s.addLast(15);
        s.addLast(30);
        s.addLast(40);
        s.addLast(50);
        s.addLast(60);
        s.addLast(70);
        s.addLast(80);
        s.moveMinToFront();
        assertEquals(15, (int)s.first());
        assertEquals(8, (int)s.size());
        assertEquals(80, (int)s.last());
    }

    @Test
    public void moveMinToFrontTest3() {
        IntSinglyLinkedList s = new IntSinglyLinkedList();
        s.addFirst(10);
        s.addFirst(20);
        s.addFirst(30);
        s.addFirst(40);
        s.addFirst(50);
        s.moveMinToFront();
        assertEquals(10, (int)s.first());
        assertEquals(5, (int)s.size());
        assertEquals(20, (int)s.last());
    }

    @Test
    public void moveMinToFrontTest4() {
        IntSinglyLinkedList s = new IntSinglyLinkedList();
        s.addFirst(20);
        s.addFirst(15);
        s.addFirst(30);
        s.addFirst(40);
        s.addFirst(50);
        s.addFirst(60);
        s.addFirst(70);
        s.addFirst(80);
        s.moveMinToFront();
        assertEquals(15, (int)s.first());
        assertEquals(8, (int)s.size());
        assertEquals(20, (int)s.last());
    }

    @Test
    public void moveMinToFrontTest5() {
        IntSinglyLinkedList s = new IntSinglyLinkedList();
        Random r = new Random(111);
        int min = Integer.MAX_VALUE;
        int last = Integer.MAX_VALUE;
        final int lengthOfList = 100;
        for (int i=0; i<lengthOfList; i++) {
            int x = r.nextInt(2000);
            min = Math.min(x, min);
            s.addLast(x);
            last = x;
        }
        s.moveMinToFront();
        assertEquals(min, (int)s.first());
        assertEquals(lengthOfList, (int)s.size());
        assertEquals(last, (int)s.last());
    }
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

class Node{

    private int data;

    private Node next;

    public Node(){}

    public Node(int val){

        data = val;

        next = null;

    }

    public Node(int val, Node _next){

        data = val;

        next = _next;

    }

    /**

     * @return the data

     */

    public int getData() {

        return data;

    }

    /**

     * @return the next

     */

    public Node getNext() {

        return next;

    }

    /**

     * @param data the data to set

     */

    public void setData(int data) {

        this.data = data;

    }

    /**

     * @param next the next to set

     */

    public void setNext(Node next) {

        this.next = next;

    }

}

public class IntSinglyLinkedList{

    private Node start;

    public IntSinglyLinkedList(){

        start = null;

    }

    // some other methods...

    public void moveMinToFront() {

        Node temp = start;

        Node prev = null;

        Node minNode = start;

        Node prevMin = null;

        while(temp!=null){

            if(minNode.getData() > temp.getData()){

                minNode = temp;

                prevMin = prev;

            }

            prev = temp;

            temp = temp.getNext();

        }

        if(temp!=start){

            prevMin.setNext(minNode.getNext());

            minNode.setNext(start);

            start = minNode;

        }

    }

}


Related Solutions

Explain why the Java dynamic dispatch algorithm, which looks for the method to invoke for a...
Explain why the Java dynamic dispatch algorithm, which looks for the method to invoke for a call o.a(), will never get into an infinite loop.
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Please implement the java method addInOrder() that allows you to create and maintain the lists in...
Please implement the java method addInOrder() that allows you to create and maintain the lists in the order required. The addInOrder method must work with different external Comparator objects. (Hint: Consider creating and using a private compare method and a private Comparator reference as members of your SLL class. If your SLL is constructed without any parameter, then you should initialize the internal Comparator object reference to null. Otherwise, you should initialize it to the external Comparator object passed as...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT