Question

In: Computer Science

In java write a method that will take an array and change it into a linkedlist...

In java write a method that will take an array and change it into a linkedlist and then display it in the main method

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

class LinkedList {
        // head node
        private Node head;

        // inner node class
        private class Node {
                int data;
                Node next;

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

        // method to convert array to linkedlist
        public void arrayToLinkedList(int[] data) {
                // head node points to first element
                head = new Node(data[0]);
                Node last = head;
                // looping through the array
                for (int i = 1; i < data.length; i++) {
                        // looping through the list
                        while (last.next != null)
                                last = last.next;
                        // adding element at the end
                        last.next = new Node(data[i]);
                }

        }

        // for printing the linkedlist
        public void printList() {
                Node currentNode = head;
                while (currentNode != null) {
                        System.out.print(currentNode.data + " ");
                        currentNode = currentNode.next;
                }
        }
}

public class Test {
        public static void main(String[] args) {
                // testing the method
                int[] ar = { 5, 9, 3, 7, 2 };
                LinkedList llist = new LinkedList();
                llist.arrayToLinkedList(ar);
                llist.printList();
        }
}

Output


Related Solutions

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...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
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.
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
What is the difference between Array and Linkedlist. What is Array with example? What is Linkedlist...
What is the difference between Array and Linkedlist. What is Array with example? What is Linkedlist with example? What is the difference?
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)
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
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 code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT