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(""+getMin(list));
   }

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

}

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
                }
        }
}

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("" + getMin(list));
        }

        public static int getMin(ACOLinkedList list) {
                Node trav = list.head;
                if (trav == null)
                        return -1;
                int min = trav.data;
                while (trav != null) {
                        if (min > trav.data)
                                min = trav.data;
                        trav = trav.next;
                }
                return min;
        }

}

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...
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...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT