Question

JAVA QUESTION: A singly linked list method that will: -A method that will find the largest...

JAVA QUESTION: A singly linked list method that will:

-A method that will find the largest number in the list

-A method that will find the smallest number in the list

THESE ARE TWO SEPARATE BASIC METHODS

Homework Answers

Answer #1

CODE:   The above two methods are highlighted in my code.


import java.io.*;
import java.util.*;
public class LinkedList {
  
class Node {
  
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public Node head = null;
public void insert(int data)
{   
  
Node new_node = new Node(data);
new_node.next = null;
if (head == null) {
head = new_node;
}
else {
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}

}

public void print() {
Node current = head;
  
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
  
public void Largest() {
Node curr = head;
// Declaring a max variable and initializing
// it with INT_MIN value.
int max=Integer.MIN_VALUE;
  
if(head == null) {
System.out.println("List is empty");
}
else {
max = head.data;
while(curr != null){
// If max is less then curr->data then
// assign value of curr->data to max
// otherwise node point to next node.
if(max < curr.data) {
max = curr.data;
}
curr = curr.next;
}   
System.out.println("Largest value node in the list: "+ max);
}
}
  
public void Smallest() {
Node curr = head;
// Declaring a min variable and initializing
// it with INT_MAX value.
int min=Integer.MAX_VALUE;
  
if(head == null) {
System.out.println("List is empty");
}
else {
min = head.data;
while(curr != null){
// If min is greater then curr->data then
// assign value of curr->data to min
// otherwise node point to next node.
if(min > curr.data) {
min = curr.data;
}
curr= curr.next;
}
System.out.println("Smallest value node in the list: "+ min);
}
}

public static void main(String[] args) {
LinkedList sLL = new LinkedList();
  
sLL.insert(11);
sLL.insert(2);
sLL.insert(38);
sLL.insert(0);
sLL.insert(76);
sLL.insert(22);
sLL.Largest();
sLL.Smallest();
sLL.print();
}

}

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                                                                  void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref;...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n elements. The program gets the head of the list (L.head) as input and cannot use any storage other than the given list. Don't not use recursive~~~~~~~ thanks
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
Write a C++ recursive function that counts the number of nodes in a singly linked list....
Write a C++ recursive function that counts the number of nodes in a singly linked list. (a) Test your function using different singly linked lists. Include your code. (b) Write a recurrence relation that represents your algorithm. (c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does no have tail? Write the code. How much time does it take to do it? (java)
Implementing Polynomials using Singly Linked List in C++
Implementing Polynomials using Singly Linked List in C++
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT