Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.NoSuchElementException; import java.util.Scanner; /** * Questio

ID: 3691374 • Letter: I

Question

import java.util.NoSuchElementException;
import java.util.Scanner;

/**
* Question 8:
*
* Finish the implementation of the findMax() method.
* This method will return the largest integer in the list
*
* For instance, given the list
* 4 5 27 6
* findMax() would return 27
*
* If the list is empty, findMax should throw a "NoSuchElementException"
*
* You will also need to add the "addToEnd()" method that you
* wrote in question 5. You can simply copy and paste that
* into this class.
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*
* or immediately after
* "Paste addToEnd() method here"
*/

public class LinkedList08 {
   // Private inner class Node
  
   private class Node{
       int data;
   Node link;
  
   @SuppressWarnings("unused")
       public Node(){
       data = Integer.MIN_VALUE;
       link = null;
   }
  
   public Node(int x, Node p){
       data = x;
       link = p;
   }
   }
   // End of Node class
  
   public Node head;
  
   public LinkedList08(){
       head = null;
   }
  
   public int findMax() throws NoSuchElementException {
       /** Begin your code here **/
      
       /** end your code here **/
   }
  
   /** Paste addToEnd() method here **/
   public void addToEnd(int data) {
       /** Begin your code here **/
       if(head == null)
       {
       head = new Node(data, head);
       }
       else
       {
       Node position = head;
       while(position.link != null)
       {
       position = position.link;
       }
       position.link = new Node(data, null);
       }
       /** end your code here **/
   }

   public void printList(){
       Node position = head;
       if (head == null)
           System.out.println("The list is empty.");
       while(position != null){
           System.out.print(position.data + " ");
           position = position.link;
       }
   }
  
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       LinkedList08 list = new LinkedList08();
      
       System.out.println("How many values do you want to add to the list?");
       int toAdd = input.nextInt();
       for(int i = 0; i < toAdd; i++) {
           System.out.println("Enter value " + (i + 1));
           list.addToEnd(input.nextInt());
       }
       System.out.println("Here is the list");
       list.printList();
      
       System.out.println("The largest number in the list is " + list.findMax());
      
       input.close();

   }

}

Explanation / Answer

Linkedlist.java

public class Linkedlist {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        List a=new List();
        a.addToEnd(1);
        a.addToEnd(2);
        a.addToEnd(20);
        a.addToEnd(12);
        a.addToEnd(100);
      
        a.addToBeginning(0);
        a.addToBeginning(-1);
        a.print();
        System.out.println("Largest Item in a Linked List: "+a.findMax());
    }
  
}

List.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class List {
    private Node head;
    public List(){
        this.head=null;
    }
//ADD New Node to the end of a List
    public void addToEnd(int a)
    {
        Node tmp = new Node(a);
        if (head==null)
            head=tmp;
        else
        {
            Node r=head;
            while(r.getNext()!=null)
            {
                r=r.getNext();
            }
        r.setNext(tmp);  
        }      
    }
//Add node to the beginning of list  
    public void addToBeginning(int a)
    {
        Node tmp =new Node(a);
        if(head==null)
            head=tmp;
        else
        {
            tmp.setNext(head);
            head=tmp;
        }
    }
//find the largest Item;
    public int findMax(){
        int max=head.getData();
      
        Node tmp=head;
        while (tmp!=null){
            if (max<tmp.getData())
                max=tmp.getData();
       
            tmp=tmp.getNext();
        }
        return max;
    }
//Printing list
    public void print(){
    Node r;
    r=head;
    while(r!=null)
    {
        System.out.println(r.getData());
        r=r.getNext();
    }
  
    }
  
  
  
}


Node.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class Node {
    private Node head;
    private Node next;
    private int data;
    public Node(int data){
        this.data=data;
        this.next=null;
    }
    /**
     * @return the head
     */
    public Node getHead() {
        return head;
    }

    /**
     * @param head the head to set
     */
    public void setHead(Node head) {
        this.head = head;
    }

    /**
     * @return the next
     */
    public Node getNext() {
        return next;
    }

    /**
     * @param next the next to set
     */
    public void setNext(Node next) {
        this.next = next;
    }

    /**
     * @return the data
     */
    public int getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(int data) {
        this.data = data;
    }
  
}

sample output

-1                                                                                                                                                          
0                                                                                                                                                           
1                                                                                                                                                           
2                                                                                                                                                           
20                                                                                                                                                          
12                                                                                                                                                          
100                                                                                                                                                         
Largest Item in a Linked List: 100