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
Solution:
/*save file name as TestLinkedList.java
since public class should be saved as file name
*/
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;
}
}
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;
}
//to find maximum value definition of findmax() method
public int findMax()
{
int num=Integer.MIN_VALUE;
while(first.getNumber()>num)
{
num=first.getNumber();
first=first.getNext();
}
//System.out.println(num);
return num;
}
}
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();
//res is integer type variable to store maximum value return by findMax()
//declaration of res
int res ;
res =list.findMax();// function findMax() called
//printing of maximum value stored in res variable
System.out.println("The maximum number is: " + res);
}
}
The above code is implementation of Linked list with some function like InsertFirst,DisplayList and many which is given.
Now It also Include FindMax() method That return maximum number of list.
OUTPUT
The above image showing the output for above implemented code for linkedList
data inserted to link list is 4,12,6,10,2
and findMax() method return maximum value as 12 since 12 is maximum value.
OUTPUT2.
TThe above image is showing the details for implementation of Linked list of above code
displayList() ,display the data of linked list and findMax() method return maximum data value
Here Inserted data are 6,55,45,10,20
and maximum data value is 55.
/* If any confusion please comment kindly */