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...
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;...
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)
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...
Given a list of n integers, find the largest integer in the list. Use Java or...
Given a list of n integers, find the largest integer in the list. Use Java or C++ please
#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 <<...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Please answer quickly! Thank you. Java: Write a non-static method to be added to a class...
Please answer quickly! Thank you. Java: Write a non-static method to be added to a class that implements a singly linked list of integers. The method is called make_partition() and takes an integer x as input. The method should modify this list by moving all the elements less than x to the front of the list, and all the elements greater than x to the back of the list. It is not the important for the elements to be kept...
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 not have tail? Write the code. How much time does it take to Do it?