Question

In: Computer Science

import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...

import java.io.*;
import java.util.Scanner;

class Node {
int data;
Node next;
Node(int d){ // Constructor
   data = d;
   next = null;
}
}

class ACOLinkedList {// a Singly Linked List
   Node head; // head of list
   public void insert(int data){ // Method to insert a new node
       Node new_node = new Node(data); // Create a new node with given data
       new_node.next = null;
       if (head == null) // If the Linked List is empty, then make the new node as head
           head = new_node;
       else {// Else traverse till the last node and insert the new_node there
           Node last = head;
           while (last.next != null)
               last = last.next;
           last.next = new_node; // Insert the new_node at last node
       }
   }
}

class Main {
public static void main(String[] args)
   {
       ACOLinkedList list = new ACOLinkedList();/* Start with the empty list. */
       Scanner scan = new Scanner(System.in);
       int num;
       for (int i=0; i<10; i++){//Read list values
           num = scan.nextInt();
           list.insert(num);
       }
System.out.println(""+getSecondMax(list));
   }

   
public static int getSecondMax(ACOLinkedList list) {
       //TODO: Find the second largest element in the list (list could have any number of elements)
       //Example: if list={40, 20, 25, 15, 99}, the method should return: 40
  
      
   }

}

Solutions

Expert Solution

import java.io.*;
import java.util.Scanner;

class Node {
   int data;
   Node next;

   Node(int d) { // Constructor
       data = d;
       next = null;
   }
}

class ACOLinkedList {// a Singly Linked List
   Node head; // head of list

   public void insert(int data) { // Method to insert a new node
       Node new_node = new Node(data); // Create a new node with given data
       new_node.next = null;
       if (head == null) // If the Linked List is empty, then make the new node as head
           head = new_node;
       else {// Else traverse till the last node and insert the new_node there
           Node last = head;
           while (last.next != null)
               last = last.next;
           last.next = new_node; // Insert the new_node at last node
       }
   }
}

public class Main {
   public static void main(String[] args) {
       ACOLinkedList list = new ACOLinkedList();/* Start with the empty list. */
       Scanner scan = new Scanner(System.in);
       int num;
       for (int i = 0; i < 10; i++) {// Read list values
           num = scan.nextInt();
           list.insert(num);
       }
       System.out.println("" + getSecondMax(list));
   }

   public static int getSecondMax(ACOLinkedList list) {
       Node trav = list.head;
       int max = -1, secondMax = -2;
       while (trav != null) {
           if (max < trav.data) {
               secondMax = max;
               max = trav.data;
           } else if (secondMax < trav.data) {
               secondMax = trav.data;

           }
           trav = trav.next;
       }
       return secondMax;
   }

}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT