Question

In: Computer Science

In java we will need to create a method that takes the array {1,2,3,4,5} and returns...

In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.

Solutions

Expert Solution

Code:

public class LinkedListDisplay {

   
public static void main(String[] args) {

        LinkedListDisplay listDisplay =
new LinkedListDisplay();
        Node head = listDisplay
            .generateLinkedList(
new int[]
            {
               
1, 2, 3, 4, 5
           
});

       
// display the list.
       
Node temp = head;
       
while (temp != null) {

            System.
out.print(temp.item + " ");
            temp = temp.
next;

        }

        System.
out.println();
    }

   
private Node generateLinkedList(int[] elements) {

        Node head =
new Node(elements[0]);
        Node temp = head;

       
for (int i = 1; i < elements.length; ++i) {
            Node newNode =
new Node(elements[i]);
            temp.
next = newNode;
            temp = newNode;
        }

       
return head;

    }

   
class Node {

       
int item;
        Node
next;

        Node(
int item) {
           
this.item = item;
        }
    }

}

Snapshot of the output:

Snapshot of the code:

Let me know if anything is not clear.


Related Solutions

Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Create global variables Mean, Mode, Median, then create a method that takes an array of 10...
Create global variables Mean, Mode, Median, then create a method that takes an array of 10 double numbers and return three answers of the Mean, Median, Mode ( no need for implementation of the mean, median and mode, calculate them manually and return the answers), assign the answers to the global variables. In java, please!!
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns a created two-dimension array. var twoD = Make2D(<width>, <height>); Challenge 2 – Fill 2D Array Create a function that takes a single parameter and fills the 2D array with that parameter Use Cases: Fill2D(<twoD array>, <fill value>); //fills twoD twoD = fill2D(<twoD array>, <fill value>); //fills twoD, but also returns reference Challenge 3 – Make 2D Array Write a function that takes 3 parameters...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT