Question

In: Computer Science

(Java) Create a new linked list from two given arrays with the greater element from each...

(Java)

Create a new linked list from two given arrays with the greater element from each corresponding array element placed into the linked list.

Given two arrays of varying size initialized with integers of varying values, the task is to create a new linked list using those arrays. The condition is that the greater element value from each corresponding array element will be added to the new linked list in the list position that maintains the integers in ascending order. When the program has reached the end of the shorter array, the remaining elements of the longer array will be added to the linked list in such a way as to maintain the integers in the linked list in ascending order.

Solutions

Expert Solution

Java code:

public class LinkedList {
    Node head = null;

    static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
            this.next = null;
        }
    }

    public static void main(String[] args) {
        int[] a1 = {2, 5, 3, 9, 11};
        int[] a2 = {5, 67, 33, 0, 7, 9, 4};
        LinkedList list = new LinkedList();
        int ind1 = 0, ind2 = 0;
        while (ind1 < a1.length && ind2 < a2.length) {
            if (a1[ind1] >= a2[ind2]) {
                list.add(a1[ind1]);
            } else
                list.add(a2[ind2]);
            ind1++;
            ind2++;
        }
        //Adding left over nodes if any
        for (; ind1 < a1.length; ind1++)
            list.add(a1[ind1]);
        for (; ind2 < a2.length; ind2++)
            list.add(a2[ind2]);
        list.display();
    }

    /**
     * 
     * @param val Adds the value in appropriate position in the list
     */
    public void add(int val) {
        Node curr = head;
        if (curr == null) {
            head = new Node(val);
            return;
        }
        if(val<head.value) {
            Node n = new Node(val);
            n.next = head;
            head = n;
            return;
        }
        Node prev = null;
        while (curr != null && curr.value < val) {
            prev = curr;
            curr = curr.next;

        }
        if (curr != null) {
            Node temp = curr;
            prev.next = new Node(val);
            prev.next.next = temp;
        }
        else {
            prev.next = new Node(val);
        }
    }

    /**
     * Prints the numbers in list
     */
    public void display() {
        Node curr = head;
        while (curr != null) {
            System.out.println(curr.value + " ");
            curr = curr.next;
        }
    }
}

Output:

4
5
9
9
11
33
67


Please let me know if any tweaks are requried. If not, do give a thumbs up


Related Solutions

**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Create a Linked List and conduct the following operations. Portion of the program is given. The...
Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add 100 to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print it...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT