Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. package practicaIntegradora;
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.LinkedBlockingDeque;
  5.  
  6. public class ColaDuplicada {
  7.     public static void main(String[] args) {
  8.         System.out.println("Initializing queue...");
  9.         Deque<Integer> queue = new LinkedBlockingDeque<>();
  10.         System.out.println("queue initialized. elems: " + queue);
  11.  
  12.         System.out.println("");
  13.         System.out.println("Queuing 10 numbers...");
  14.         for(int i = 1; i <=10; i++) {
  15.             System.out.println("Queuing " + i);
  16.             queue.addLast(i);
  17.             System.out.println("Queue: " + queue);
  18.         }
  19.        
  20.         System.out.println("");
  21.         System.out.println("Adding elements from first queue into a duplicate queue, and duplicating their value");
  22.         Deque<Integer> duplicatedQueue = new ArrayDeque<>();
  23.         while(!queue.isEmpty()) {
  24.             Integer element = queue.removeFirst();
  25.             System.out.println("Adding element with a duplicated value: " + element);
  26.             duplicatedQueue.addLast(element*2);
  27.             System.out.println("First queue after removal: " + queue );
  28.             System.out.println("Duplicate queue after adding element: " + duplicatedQueue );
  29.         }
  30.        
  31.         System.out.println("");
  32.         System.out.println("Extracting elements from duplicated queue until we find an odd number");
  33.         while(!duplicatedQueue.isEmpty()) {
  34.             Integer element = duplicatedQueue.peekFirst();
  35.             if(element % 2 == 0) {
  36.                 duplicatedQueue.remove();
  37.             }
  38.             System.out.println("Duplicate queue after removing element until finding an odd one: " + duplicatedQueue );
  39.            
  40.         }
  41.        
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement