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}
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.
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT