Question

In: Computer Science

The following program creates a linked list which contains 5 links. Add a method called findMax()...

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

Solutions

Expert Solution

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.


Related Solutions

The following program creates a linked list which contains 5 links. Add a method called findMax()...
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...
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a...
Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array.
1. Which of the following is a method that can be called on a list? Group...
1. Which of the following is a method that can be called on a list? Group of answer choices split() lower() strip() All of the above methods can be called on a list None of the above 2. Which one of these correctly reads a value from the keyboard into variable x?   Group of answer choices input(x) readline(x) x = input() x = readline() 3. What happens if you run the following code?   x = input("Enter a value: ") if...
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
I am trying to add two linked-list based integers, but the program keeps giving me the...
I am trying to add two linked-list based integers, but the program keeps giving me the incorrect result. I am trying to add (List 1: 43135) + (List 2: 172). I have pasted the code below #include <iostream> using namespace std; //Linked list node class Node {    public:    int num;    Node* next; }; //Function to create a new node with given numbers Node *new_Node(int num) {    Node *newNode = new Node();    newNode->num = num;   ...
Change program linkedListClass to the linked list for student items (a student item contains id, name...
Change program linkedListClass to the linked list for student items (a student item contains id, name and score, where the id is used as key) and test it in the main method. You can define a student item using a class of student. given the following codes; import java.util.*; public class linkedListClass {    public Node header;    public linkedListClass()    {        header = null;    }    public final Node Search(int key)    {        Node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT