Question

In: Computer Science

Using the ListNode file. Write methods called min and max that return the smallest and largest...

Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value.

Write a method called insertNode that inserts a new node anywhere in your linked list. Display your linked list with new value.

Write a program to test these methods.

Java langauge

public class ListNode
{
public int data;//data stored in this node
public ListNode front;//points to head node
public ListNode next; // link to next node in the list

// post: constructs a node with data 0 and null link
public ListNode() {
this(0, null);
}

// post: constructs a node with given data and null link
public ListNode(int data) {
this(data, null);
}

// post: constructs a node with given data and given link
public ListNode(int data, ListNode next) {
this.data = data;
this.next = next;
}

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;
}
}

// post: appends the given value to the end of the list
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);
}
}
// post : returns the position of the first occurrence of the given
// value (-1 if not found)   
public void remove(int index) {
if (index == 0) {
front = front.next;
} else {
ListNode current = nodeAt(index - 1);
current.next = current.next.next;
}
}
// pre : 0 <= i < size()
// post: returns a reference to the node at the given index
public ListNode nodeAt(int index) {
ListNode current = front;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
}

Solutions

Expert Solution

NOTE:

1. In the class ListNode both min and max functions are added.

2. For your reference, the complete program with the class containing main function is also added.

3. ScreenShots of MIN and MAX functions are also added here.

import java.lang.*;

class ListNode {

public int data;// data stored in this node

public ListNode front;// points to head node

public ListNode next; // link to next node in the list

// post: constructs a node with data 0 and null link

public ListNode() {

this(0, null);

}

// post: constructs a node with given data and null link

public ListNode(int data) {

this(data, null);

}

// post: constructs a node with given data and given link

public ListNode(int data, ListNode next) {

this.data = data;

this.next = next;

}

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;

}

}

// post: appends the given value to the end of the list

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);

}

}

// post : returns the position of the first occurrence of the given

// value (-1 if not found)

public void remove(int index) {

if (index == 0) {

front = front.next;

} else {

ListNode current = nodeAt(index - 1);

current.next = current.next.next;

}

}

// pre : 0 <= i < size()

// post: returns a reference to the node at the given index

public ListNode nodeAt(int index) {

ListNode current = front;

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

current = current.next;

}

return current;

}

// post: returns the maximum value in the Linked List

public int Max() {

ListNode current = front;

int MaxVal = current.data;

while (current.next != null) {

if (MaxVal < current.data) {

MaxVal = current.data;

}

current = current.next;

}

return MaxVal;

}

// post: returns the minimum value in the Linked List

public int Min() {

ListNode current = front;

int MinVal = current.data;

while (current.next != null) {

if (MinVal > current.data) {

MinVal = current.data;

}

current = current.next;

}

return MinVal;

}

}

class MainClass {

public static void main(String args[]) {

// Scanner sc = new Scanner(System.in);

ListNode l1 = new ListNode();

l1.add(11);

l1.add(-7);

l1.add(3);

l1.add(42);

l1.add(0);

l1.add(14);

System.out.println(l1);

System.out.println("Maximum Value : " + l1.Max());

System.out.println("Minimum Value : " + l1.Min());

}

}

OUTPUT:

[11, -7, 3, 42, 0, 14]
Maximum Value : 42
Minimum Value : -7


Related Solutions

Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
Method calling in c# I need to write methods for calling the min and max numbers...
Method calling in c# I need to write methods for calling the min and max numbers using this code; Console.WriteLine("Calling highest method."); Console.WriteLine("Highest number is: {0}", highest(3)); Console.WriteLine("Calling lowest method."); Console.WriteLine("Lowest number is: {0}", lowest(3));
Write a static method called max that creates a Scanner and connects to a file “data.txt”....
Write a static method called max that creates a Scanner and connects to a file “data.txt”. Use an exception handling  mechanism of your choice. The method should read the file until the end of time and consume tokens that could be integer,  double or string type. It should count and return the number of words that are not integer or doubles.
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
In MIPS assembly language, write a function that will display the max and min value in...
In MIPS assembly language, write a function that will display the max and min value in an array. Then write a function to calculate and display the average of all values in an array. This must be done in MIPS assembly language.
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of...
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns. There will be no error checking required for this lab. You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing): Name Height(m) Weight(kg) Joe 1.82 72.57 Mary...
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT