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

Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
**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...
In JAVA, students will create a linked list structure that will be used to support a...
In JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...
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...
I need to create a linked list that contains a fixed arraylist. Each new entry is...
I need to create a linked list that contains a fixed arraylist. Each new entry is added to array list. If the arraylist is full, create a new arraylist and add to the linklist. In java please.
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT