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

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 */


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