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,...
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...
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; //...
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).
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...
Create a Linked List and conduct the following operations. Portion of the program is given. The...
Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add 100 to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print it...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT