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
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
ADVERTISEMENT