Question

java write a method to to find the negative numbers in L1 and L2 (linked list)...

java

write a method to to find the negative numbers in L1 and L2 (linked list) and put them in L3 .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Iterator;

import java.util.LinkedList;
import java.util.Random;

public class ListDemo {
  
   private static LinkedList<Integer> L1;           // Defining L1 as LinkedList of Integers
   private static LinkedList<Integer> L2;           // Defining L2 as LinkedList of Integers
   private static LinkedList<Integer> L3;           // Defining L3 as LinkedList of Integers

  
   /*
   * main method to
   * 1. populate the lists (L1 and L2) with random positive and negative numbers
   *       extract negative numbers from L1 and L2 and putting them in L3
   * 2. print L1, L2 and L3
   */
   public static void main(String[] args) {
       L1 = new LinkedList<Integer>();               //Instantiating L1
       L2 = new LinkedList<Integer>();               //Instantiating L2  
      
       L3 = new LinkedList<Integer>();               //Instantiating L3
      
       popualateLists(L1, L2, L3);
      
       printLists(L1, L2, L3);
   }

  
   /*
   * method to populate the lists (L1 and L2) with random positive and negative numbers and add the
   * negative numbers from them to L3
   */
   private static void popualateLists(LinkedList<Integer> L1, LinkedList<Integer> L2, LinkedList<Integer> L3) {
      
       //populate the lists (L1 and L2) with random positive and negative numbers
       Random random = new Random();
       for (int i = 0; i < 10 ; i++) {
           L1.add(random.nextInt(50) * (random .nextBoolean() ? -1 : 1));
           L2.add(random.nextInt(50) * (random .nextBoolean() ? -1 : 1));
       }
      
      
      
       //extract negative numbers from L1 and L2 and putting them in L3
       Iterator<Integer> iterator1 = L1.iterator();
       Iterator<Integer> iterator2 = L2.iterator();
      
       //adding negative numbers from L2 to L3
       Integer num;
       while (iterator1.hasNext()) {
           num = iterator1.next().intValue();
           if(num < 0) {
               L3.add(num);
           }
       }
      
       //adding negative numbers from L2 to L3
       while (iterator2.hasNext()) {
           num = iterator2.next().intValue();
           if(num < 0) {
               L3.add(num);
           }
       }
   }

   //method to print L1, L2 and L3 for testing purpose
   private static void printLists(LinkedList<Integer> L1, LinkedList<Integer> L2, LinkedList<Integer> L3) {
      
       Iterator<Integer> iteratorl1 = L1.iterator();
       Iterator<Integer> iteratorl2 = L2.iterator();
       Iterator<Integer> iteratorl3 = L3.iterator();
      
       // printing L1
       System.out.println("Numbers in L1: ");
       while (iteratorl1.hasNext()) {
           System.out.print(iteratorl1.next().intValue() + " ");
          
       }
      
       // printing L2
       System.out.println("\n\nNumbers in L2: ");
       while (iteratorl2.hasNext()) {
           System.out.print(iteratorl2.next().intValue() + " ");
       }
      
       // printing L3
       System.out.println("\n\nNumbers in L3 after putting negative numbers from L1 and L2 : ");
       while (iteratorl3.hasNext()) {
           System.out.print(iteratorl3.next().intValue() + " ");
       }
   }
}

SCREENSHOTS :

Add a comment
Know the answer?
Add Answer to:
java write a method to to find the negative numbers in L1 and L2 (linked list)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • Java - Write an insertBefore method for a List implemented as a Linked List that contains...

    Java - Write an insertBefore method for a List implemented as a Linked List that contains two dummy nodes. - Write an insertAfter method for a List implemented as a Linked List that contains two dummy nodes.

  • 5. Write KVL for L1, L2 and L3. Write KCL for nodes A and B. Identify...

    5. Write KVL for L1, L2 and L3. Write KCL for nodes A and B. Identify other possible nodes and write KCL for them. Identify other possible loops and write KVL for them. R1 R4 Vs2 R2

  • Given two sorted lists, L1 and L2, write a C++ function to compute L1 ∪ L2...

    Given two sorted lists, L1 and L2, write a C++ function to compute L1 ∪ L2 using only the basic list operations.

  • Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as...

    Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as a new list. 1) Implement a method: mergeTwoLists(…){…} 2) Use the LinkedList library in Java 3) Test your method. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 ````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` import java.util.*; public class Inclass1 { public static void main(String[] args){ //Question 5.2 LinkedList l1 = new LinkedList<>(); LinkedList l2 = new LinkedList<>(); l1.add(1); l1.add(2); l1.add(10); l2.add(1); l2.add(3); l2.add(4); LinkedList l3 = mergeTwoLists(l1, l2); System.out.println("Question \n******************************"); for(int i...

  • Write a JAVA contains method for a linked implementation of a sorted list. #This method is...

    Write a JAVA contains method for a linked implementation of a sorted list. #This method is written from the implementation perspective, meaning it would go inside of the LinkedSortedList class. This means we have access to firstNode and numberOfEntries. #write an efficient solution. This will involve directly accessing the linked structure rather than only invoking existing methods. You can assume T is Comparable. #The method header is: public boolean contains(T anEntry)

  • 1)Given two lists L1 and L2, create a new list L3 consisting of the first element...

    1)Given two lists L1 and L2, create a new list L3 consisting of the first element of L1 and the last element of L2. For example, if L1=[1,2,3] and L2=["a","b","c"], L3 should be [1,"c"] 2)given a string s, create a new string odd_letters that contains only the odd letters of s (two ways to solve this problem are to use slicing, or a while or for loop), i.e. starting at the first letter or the zeroth index: if s="banana", odd_letters...

  • Code in Java to be typed in Eclipse. list. Then create another ArrayList L2, with all...

    Code in Java to be typed in Eclipse. list. Then create another ArrayList L2, with all the prime numbers in the list L1. Use an enhanced for-loop instead of an ordinary for-loop in your program

  • Please do this in Java!!! Describe a method for performing a card shuffle of a singly...

    Please do this in Java!!! Describe a method for performing a card shuffle of a singly linked list of 2n elements, by converting it into two lists. A card shuffle is a permutation where a list L is cut into two lists, L1 and L2, where L1 is the first half of L and L2 is the second half of L, and then these two lists are merged into one by taking the first element in L1, then the first...

  • Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and...

    Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and returns two lists Li and L2, where Li contains all elements in L for which PredName (x) fails, and L2 contains all elements in L for which PredName (x) succeeds. The predicate PredName/1 should be defined when calling the procedure filter. For example: let test be defined as test(N).- N > 0. 7- filter((-6,7,-1,0),test,L1,L2). L1 - (-6.-1) L2 - [7, 0] NB Use the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT