Write a method called min that returns the minimum value in a list of integers.
ID: 3568794 • Letter: W
Question
Write a method called min that returns the minimum value in a list of integers. If the list is empty, it should throw a NoSuchElementException. Please implement the list with these values [2, 6, 3, 18, 4, 15]. The new method should be added to this LinkedIntList class.
public class LinkedIntList {
private ListNode front;
public LinkedIntList() ;
front = null;
}
public int size() {
int count = 0;
ListNode current = front;
while (current != null) {
current - current.next;
count++;
}
return count;
}
public int get(int index) {
return nodeAt(index).data;
}
public String toString() {
if (front == null) {
return "[]";
} else {
String result = "[" + front.data;
ListNode current = front.next;
while (current != null) {
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
public int indexOf(int value) {
int index = 0;
ListNode current = front;
while (current != null) {
if (current.data == value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
public void add(int value) {
if (front == null) {
front = new ListNode(value);
} else {
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
}
public void add(int index, int value) {
if (index == 0) {
front = new ListNode(value, front);
} else {
ListNode current = nodeAt(index - 1);
current.next = new ListNode(value, current.next);
}
}
public void remove(int index) {
if (index == 0) {
front = front.next;
} else {
ListNode current = nodeAt(index - 1);
current.next = current.next.next;
}
}
private ListNode nodeAt(int index) {
ListNode current = front;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
}
Explanation / Answer
import java.util.*;
public class ListNode{
public int data;
public ListNode next;
public ListNode(int v){
data = v;
next = null;
}
public ListNode (int v, ListNode n)
{
data = v;
next = n;
}
};
public class LinkedIntList {
private ListNode front;
public LinkedIntList(){
front = null;
}
public int size() {
int count = 0;
ListNode current = front;
while (current != null) {
current = current.next;
count++;
}
return count;
}
public int get(int index) {
return nodeAt(index).data;
}
public String toString() {
if (front == null) {
return "[]";
} else {
String result = "[" + front.data;
ListNode current = front.next;
while (current != null) {
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
public int indexOf(int value) {
int index = 0;
ListNode current = front;
while (current != null) {
if (current.data == value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
public void add(int value) {
if (front == null) {
front = new ListNode(value);
} else {
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
}
public void add(int index, int value) {
if (index == 0) {
front = new ListNode(value, front);
} else {
ListNode current = nodeAt(index - 1);
current.next = new ListNode(value, current.next);
}
}
public void remove(int index) {
if (index == 0) {
front = front.next;
} else {
ListNode current = nodeAt(index - 1);
current.next = current.next.next;
}
}
private ListNode nodeAt(int index) {
ListNode current = front;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
// Write a method called min that returns the minimum value in a list of integers.
public int min() throws NoSuchElementException{
// If the list is empty, it should throw a NoSuchElementException.
if(front == null)
throw new NoSuchElementException();
int local = front.data;
ListNode current = front.next;
while (current != null) {
if(current.data < local) local = current.data;
current = current.next;
}
return local;
}
}
class Demo{
public static void main(String[] args){
// Please implement the list with these values [2, 6, 3, 18, 4, 15].
LinkedIntList LIL = new LinkedIntList();
LIL.add(2);
LIL.add(6);
LIL.add(3);
LIL.add(18);
LIL.add(4);
LIL.add(15);
System.out.println("Minimum element in List is " + LIL.min());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.