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...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
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...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
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
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT