Question

Write a method max() which take a generic singly linked list as an argument and returns...

Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.

     If the list is empty, please throw an empty collection exception.

     The method prototype is defined as follows.

Please write down your code in the method body enclosed in braces:

public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException {

}

2) What is the big O notation of the max method?

a) O(N)

b) O(1)

c) O(N log N)

d) O(log N)

e) O(N2)

3) If we have to sort the list, which algorithm could you use? Briefly write the pseudo code of a sort algorithm.

Thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given a single linked list of n nodes and find the smallest and largest element in linked list

java code for it-

class RONALDO

{

static class Node

{

int data;

Node next;

}

static Node head=null;

static int largestElement(Node head)

{

Int max=Integer.MIN_VALUE;

while(head!=null)

{

if(max<head.data)

max=head.data;

head=head.next;

}

return max;

}

static int smallestElement(Node head)

{

int min=Integer.MAX_VALUE;

while(head!=null)

{

if(min>head.data)

min=head.data;

head=head.next;

}

return min;

}

static void push(int data)

{

Node newNode=new Node();

newNode.data= data;

newNode.next=(head);

(head)=newNode;

}

static void printList(Node head)

{

while(head!=null)

{

System.out.println(head.data + " -> ");

head=head.next;

}

System.out.println("NULL");

}

public static void main(String[] args)

push(15);

push(14);

push(13);

push(22);

push(17);

System.out.println("Linked list is : ");

printList(head);

System.out.println("Maximum element in linked list: ");

System.out.println(largestelement(head));

System.out.print("Maximum element in Linked List: " );

System.out.print(smallestElement(head));

}

}

2-O(N2)

3- class BUBBLESORT

{

void bubblesort(int arr[])

{

int n=arr.length;

for(int i=0; i<n-1;i++)

for(int j=0;j< n-i-1;j++)

if(arr[j] >arr[j+1])

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

}

void printArray(int arr[])

{

int n=arr.length;

for(int i=0;i<n;++i)

System.out.print(arr[i] + " ");

System.out.print();

}

public static void main(String args[])

{

BubbleSort ob= new BuubleSort();

int arr[]={12,56,32,14,56,43,63};

ob.bubbleSort(arr);

System.out.println("Sorted array"):

ob.printArray(arr);

}

}

Hey!! i wrote each part answer in detail,kindly c it properly ,if any query then dm me and kindly uprate my ratings as it is the most important thing for me at the moment..

Add a comment
Know the answer?
Add Answer to:
Write a method max() which take a generic singly linked list as an argument and returns...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in...

    ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to allocate memory dynamically. What is the running time of the algorithm? b) Write pseudo code to output a singly-linked list in reverse order when you are ALLOWED to allocate memory dynamically. What is the running time of the algorithm? c) You have an increasingly-sorted circular list (using an array) of n elements that is full. The...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • 2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority...

    2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...

  • Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list...

    Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise {       public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {        // first line to start you off        ListIterator<E> iit = c.listIterator(), jit;...

  • 1. Implement Doubly Linked List get method which behaves like the operator [] of array. -...

    1. Implement Doubly Linked List get method which behaves like the operator [] of array. - It takes an integer parameter i as the index, it throw an Exception if the index i is illegal, returns the element at given index i, it traverse from the header of the list if index i is in the lower end of the list(less than half of the size), and traverse from the trailer of the list if index i is in the...

  • iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains...

    iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...

  • Write a method max() that takes a reference to the first node in a linked list...

    Write a method max() that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty. In java

  • Can someone implement this using singly linked list. Please and thank you public interface Priori...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • The screenshot below states "The singly linked list STL container std::forward_list has an erase function, which...

    The screenshot below states "The singly linked list STL container std::forward_list has an erase function, which can be directly used here. But that function erases AFTER the item pointed to, not the one pointed to like the other containers. A singly linked list cannot look backwards, only forward. You need to convert the zero-based offset from the top to an iterator by advancing _groceries_sl_list.before_begin() offsetFromTop times. The STL has a function called std::next() that does that, or you can write...

  • In JAVA, please. THANK YOU In this project you will create a generic linked list using...

    In JAVA, please. THANK YOU In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT