In: Computer Science
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;
}
}
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