In: Computer Science
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method.
public class Link {
private int number;
private Link next;
public Link(int x)
{
number = x;
}
public void displayLink()
{
System.out.println("The number is: " + number);
}
public int getNumber()
{
return number;
}
public void setNumber(int y)
{
number =y;
}
public Link getNext()
{
return next;
}
public void setNext(Link l)
{
next=l;
}
}
public class LinkedList {
private static int size=0;
private Link first;
public LinkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(int x)
{
Link newLink = new Link(x);
newLink.setNext(first);
first = newLink;
size++;
}
public void deleteFirst()
{
first = first.getNext();
size--;
}public void displayList()
{
System.out.println("List (first-->last): ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.getNext();
}
}public Link find(int key)
{
Link current = first;
while(current.getNumber() != key)
{
if(current.getNext() == null)
{
return null;
}
else
{
current = current.getNext();
}
}
return current;
}
public int size()
{
return size;
}
}
public class TestLinkedList {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.insertFirst(2);
list.insertFirst(10);
list.insertFirst(6);
list.insertFirst(12);
list.insertFirst(4);
list.displayList();
}
}
Here is a sample run:
List (first-->last):
The number is: 4
The number is: 12
The number is: 6
The number is: 10
The number is: 2
The maximum number is: 12
I have implemented findMax() method and add this method to the LinkedList class at the end I have also highlighted the findMax() method.
1> public int findMax():- This method return maximum number from the linked list. return -1 when linked list is empty.
LinkedList class:-
class LinkedList {
private static int size=0;
private Link first;
public LinkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(int x)
{
Link newLink = new Link(x);
newLink.setNext(first);
first = newLink;
size++;
}
public void deleteFirst()
{
first = first.getNext();
size--;
}
public void displayList()
{
System.out.println("List (first-->last): ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.getNext();
}
}
public Link find(int key)
{
Link current = first;
while(current.getNumber() != key)
{
if(current.getNext() == null)
{
return null;
}
else
{
current = current.getNext();
}
}
return current;
}
public int size()
{
return size;
}
/**
* This method return maximum number from the linkedlist.return -1
if inkedlist is empty
* @return maximum number
*/
public int findMax(){
// check whether linkedlist is empty or not?
if(first == null){
System.out.println("LinkedList is empty!");
return -1;
}
// store first of linkedlist
Link curr = first;
// intialize max with firs data of Linkedlist
int max = first.getNumber();
// run the loop untill linklist does not become empty
while(curr != null){
// now compare each link number with max number
if(curr.getNumber() > max){
// store link number into the max
max = curr.getNumber();
}
// goto the next link
curr = curr.getNext();
}
// return the max value from the linkedlist
return max;
}
}
I have attached whole program which call the findMax() method from the TestLinkedList class.
Program:- (TestLinkedList.java)
// This class reprsent node of the linked list
class Link {
private int number;
private Link next;
public Link(int x)
{
number = x;
}
public void displayLink()
{
System.out.println("The number is: " + number);
}
public int getNumber()
{
return number;
}
public void setNumber(int y)
{
number = y;
}
public Link getNext()
{
return next;
}
public void setNext(Link l)
{
next=l;
}
}
// This class represent linkedlist
class LinkedList {
private static int size=0;
private Link first;
public LinkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(int x)
{
Link newLink = new Link(x);
newLink.setNext(first);
first = newLink;
size++;
}
public void deleteFirst()
{
first = first.getNext();
size--;
}
public void displayList()
{
System.out.println("List (first-->last): ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.getNext();
}
}
public Link find(int key)
{
Link current = first;
while(current.getNumber() != key)
{
if(current.getNext() == null)
{
return null;
}
else
{
current = current.getNext();
}
}
return current;
}
public int size()
{
return size;
}
/**
* This method return maximum number from the linkedlist.return -1 if inkedlist is empty
* @return maximum number
*/
public int findMax(){
// check whether linkedlist is empty or not?
if(first == null){
System.out.println("LinkedList is empty!");
return -1;
}
// store first of linkedlist
Link curr = first;
// intialize max with firs data of Linkedlist
int max = first.getNumber();
// run the loop untill linklist does not become empty
while(curr != null){
// now compare each link number with max number
if(curr.getNumber() > max){
// store link number into the max
max = curr.getNumber();
}
// goto the next link
curr = curr.getNext();
}
// return the max value from the linkedlist
return max;
}
}
/*
This class create the linkedlist and demonstrate
the methods of LinkedList and Link class
*/
public class TestLinkedList {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.insertFirst(2);
list.insertFirst(10);
list.insertFirst(6);
list.insertFirst(12);
list.insertFirst(4);
list.displayList();
// call the findMax() method which return the maximum number from the linkedlist
System.out.println("The maximum number is: "+list.findMax());
}
}
Ouptut :-
I hope you will understand the above program and code of findMax() method.
Do you feel needful and useful then please upvote me.
Thank you.