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...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
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 . Implement a method that meets the following requirements: (a) Calls mergesort to sort an...
Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT