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.

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

  • 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 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
Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
Write code that finds the largest number in data and stores it in the variable max...
Write code that finds the largest number in data and stores it in the variable max and the smallest number in data and stores it in min. Test Cases Test case #1 Expected result: max is 52.66; min is 15.53 Test case #2 Expected result: max is 56.95; min is 5.77 Test case #3 Expected result: max is 77.02; min is 24.24 Test case #4 Expected result: max is 90.48; min is 35.94.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT