Question

You are given a reference to the head node of a linked list that stores integers....

You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an instance of MyList, and gets the head node of this list. Please fill out the part that says “TODO”, that computes the minimum element in the list. For fun, please solve this problem recursively. This means that no part of your solution may include iterative loops. The list will not contain any integer greater than 2000000.

ListSmallest.java:

public class ListSmallest {

   public static void main(String [] args) {
      
       // Creating an instance of MyList.
       MyList L = new MyList();

       // Get the head of the linked list.
       ListNode head = L.getHead();

       // Create an object of this program to avoid static context.
       ListSmallest l = new ListSmallest();

       // head is the head of my linked list L.
       // TODO: please write a function to print the minimum element in this list. Please store this in the variable m.
       int m = 0; // store the min in this variable.

       System.out.println("The smallest is " + m);
   }
}

ListNode.java:

public class ListNode
{
public int num;
public ListNode next;

public ListNode(int _num, ListNode _next)
{
num = _num;
next = _next;
}
}

MyList.java:

public class MyList
{
private ListNode head;

MyList()
{
System.out.println("Loading my list.");
for (int i = 0; i < 50000; ++i) {
this.head = new ListNode(200000 - i, this.head);
}
this.head = new ListNode(17, this.head);
for (int j = 50000; j < 100000; ++j)
{
this.head = new ListNode(200000 - j, this.head);
}
System.out.println("My list is successfully loaded. Please print the smallest element.");
}

public ListNode getHead()
{
return this.head;
}
}

Homework Answers

Answer #1

ListNode.java

public class ListNode

{

public int num;

public ListNode next;

public ListNode(int _num, ListNode _next)

{

num = _num;

next = _next;

}

}

Screenshot

MyList.java

public class MyList {

    private ListNode head;

    MyList() {

        System.out.println("Loading my list.");

        for (int i = 0; i < 50000; ++i) {

            this.head = new ListNode(200000 - i, this.head);

        }

        this.head = new ListNode(17, this.head);

        for (int j = 50000; j < 100000; ++j) {

            this.head = new ListNode(200000 - j, this.head);

        }

        System.out.println("My list is successfully loaded. Please print the smallest element.");

    }

    public ListNode getHead() {

        return this.head;

    }

}

Screenshot

ListSmallest.java

public class ListSmallest {

    //method which returns the smallest vlaue form a linked list

    public static int smallestVal(ListNode head){

        int small=head.num;

        //iterate List

        while (head != null) {

            if(head.num<small)

                small=head.num;

            head = head.next;

        }

        return small;

    }

   public static void main(String [] args) {

      

       // Creating an instance of MyList.

       MyList L = new MyList();

       // Get the head of the linked list.

       ListNode head = L.getHead();

       

      

       // Create an object of this program to avoid static context.

       ListSmallest l = new ListSmallest();

       // head is the head of my linked list L.

       // TODO: please write a function to print the minimum element in this list. Please store this in the variable m.

       int m = smallestVal(head); // store the min in this variable.

       System.out.println("The smallest is " + m);

   }

}

Screenshot

Output

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
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT