martes, 28 de mayo de 2013

Ejercicio pila y cola

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tarea1;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

/**
 *
 * @author Gisse
 */
public class Tarea1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Stack pila = new Stack();
        Queue cola = new LinkedList();
        Scanner sc = new Scanner(System.in);
        int a;

        System.out.println("ingrese cantidad de elementos");
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.println("Elemento " + i + " es: ");
            a = sc.nextInt();
            pila.push(a);
            cola.add(a);
        }

        System.out.println("Cola");
        for (int i = 1; i <= n; i++) {
            System.out.println("Elemento " + i + " es: " + cola.poll());
        }

        System.out.println("pila");
        for (int i = 1; i <= n; i++) {
            System.out.println("Elemento " + i + " es: " + pila.pop());
        }
    }
}

No hay comentarios:

Publicar un comentario