Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class tp3ej3 {
- public static void main(String[] args) {
- int cont =0,dimension;
- Scanner entrada = new Scanner(System.in);
- Random random = new Random();
- dimension=Helper.validarNumero(entrada, "'ingrese dimension de cola 1");
- Queue <Integer> cola1 = new Queue<Integer>(dimension);
- dimension=Helper.validarNumero(entrada, "'ingrese dimension de cola 2");
- Queue <Integer> cola2 = new Queue<Integer>(dimension);
- Queue <Integer> cola3 = new Queue<Integer>(cola1.dimensiones()+cola2.dimensiones());
- while (cont!=3){
- switch(cont){
- case 1:
- ingresarValores(cola1,entrada);
- ingresarValores(cola2, entrada);
- unirColaIntercalada(cola1, cola2,cola3);
- System.out.println(cola3.toString());
- case 2:
- ingresarValoresRandom(cola1, random);
- ingresarValoresRandom(cola2, random);
- unirColaIntercalada(cola1, cola2, cola3);
- System.out.println(cola3.toString());
- }
- cont=Helper.validarNumero(entrada, "'opcion' \n1-Manual \n2-Random \n3-salir");
- }
- }
- public static void ingresarValores(Queue<Integer> cola,Scanner entrada) {
- int cont=1;
- while (cont!=2){
- if (cola.estaLlena()){
- cola.offer(Helper.validarNumero(entrada, "ingrese un valor"));
- }
- cont=Helper.elegirOpcion(entrada, "'seguir cargando' \n1-Si \n2-No");
- }
- System.out.println("Valores ingresados"+cola.toString());
- }
- public static void unirColaIntercalada(Queue<Integer> cola1,Queue<Integer> cola2, Queue<Integer> cola3){
- boolean continuar=true;
- while (continuar){
- if(!cola1.estaVacia()){
- cola3.offer(cola1.pool());
- }
- if(!cola2.estaVacia()){
- cola3.offer(cola2.pool());
- }
- if (cola1.estaVacia() && cola2.estaVacia()){
- continuar=false;
- }
- }
- }
- public static void ingresarValoresRandom(Queue<Integer> cola,Random random){
- for (int cta = random.nextInt(10) + 1; cta > 0 ; --cta) {
- cola.offer(random.nextInt(500));
- }
- System.out.println("Valores ingresados"+cola.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment