Posts Tagged ‘listas’
Listas en Java (LinkedList)
De nuevo las listas, pero esta va vez con LinkedList, lo que permite que se agreguen o eliminen elementos en la lista al inicio o al final.
import java.util.*;
public class ListaLigada {
public static void main (String args[]) {
Scanner leer = new Scanner(System.in);
int num;
int op;
LinkedList lista = new LinkedList();
do{
System.out.println( "\t Menú \t" );
System.out.println( "Operaciones con listas" );
System.out.println( "1.- Insertar al principio" );
System.out.println( "2.- Insertar al final" );
System.out.println( "3.- Borrar al principio" );
System.out.println( "4.- Borrar al final" );
System.out.println( "5.- Mostrar la lista" );
System.out.println( "6.- Borrar toda la lista" );
System.out.println( "7.- Salir" );
System.out.println( "\n" );
System.out.println( "Elija la operación que desee" );
op = leer.nextInt();
switch(op){
case 1:
System.out.println( "Inserte numero" );
num = leer.nextInt();
lista.addFirst(num);
break;
case 2:
System.out.println( "Inserte numero" );
num = leer.nextInt();
lista.addLast(num);
break;
case 3:
System.out.println( "Se borrara el primer nodo" );
lista.removeFirst();
break;
case 4:
System.out.println( "Se borrara el nodo final" );
lista.removeLast();
break;
case 5:
System.out.println( "La lista es la siguiente" );
List lista2 = new ArrayList(lista);
Iterator it = lista2.iterator();
while (it.hasNext()){
System.out.println(it.next()+"");
}
break;
case 6:
System.out.println( "Se borraran todos los elementos de la lista" );
lista.clear();
break;
case 7:
System.out.println( "Al rato" );
break;
}
}
while( op != 7 );
}
}
Listas en Java
Bueno, este programa hace lo mismo que el anterior, solo que esta hecho de la manera “tradicional”, es decir, sin usar ArrayList o LinkedList. Se podría decir que la lista esta hecha de forma manual.
Clase Nodo
public class NLista1{
String nom;
int calif1;
int calif2;
int calif3;
double prom;
NLista1 liga;
}
Programa
import java.util.*;
public class Lista1 {
public static void main (String args[]) {
Scanner leer = new Scanner(System.in);
NLista1 inicio = null;
NLista1 fin = null;
NLista1 nuevo = null;
NLista1 aux = null;
int op;
do{
nuevo = new NLista1();
System.out.println( "Ingrese el nombre del estudiante" );
nuevo.nom = leer.next();
System.out.println( "Ingrese la primer calificación" );
nuevo.calif1 = leer.nextInt();
System.out.println( "Ingrese la segunda calificación" );
nuevo.calif2 = leer.nextInt();
System.out.println( "Ingrese la tercer calificación" );
nuevo.calif3 = leer.nextInt();
if(inicio == null){
inicio = nuevo;
nuevo.liga = null;
}
else{
fin.liga = nuevo;
nuevo.liga = null;
}
fin = nuevo;
Promedio(nuevo);
System.out.println( "¿Desea ingresar otro estudiante?" );
System.out.println( "1.-Si \t 2.-No" );
op = leer.nextInt();
}
while(op != 2);
aux = inicio;
while(aux != null){
System.out.println( "Nombre \t Promedio" );
System.out.println( aux.nom + "\t" + aux.prom );
aux = aux.liga;
}
}
public static double Promedio(NLista1 nuevo){
int suma = nuevo.calif1 + nuevo.calif2 + nuevo.calif3;
nuevo.prom = suma/3;
return nuevo.prom;
}
}
Listas en Java (ArrayList)
Aquí esta un ejemplo de listas en Java utilizando ArrayList:
Lo que hace este programa, es que pide el nombre del alumno y tres calificaciones para luego calcular su promedio. Se puede agregar cualquier cantidad de elementos a la lista.
public class NodoLista4{
String nom;
int calif1;
int calif2;
int calif3;
}
import java.util.*;
public class ListaAlumnos{
static double prom;
public static void main( String args[] ){
Scanner leer = new Scanner(System.in);
NodoLista4 nodo = new NodoLista4();
int op;
ArrayList lista = new ArrayList();
do{
System.out.println( "Ingrese el nombre del alumno:" );
nodo.nom = leer.next();
System.out.println( "Ingrese la primera calificación:" );
nodo.calif1 = leer.nextInt();
System.out.println( "Ingrese la segunda calificación:" );
nodo.calif2 = leer.nextInt();
System.out.println( "Ingrese la tercera calificación:" );
nodo.calif3 = leer.nextInt();
lista.add("Nombre del alumno:\n"+nodo.nom);
lista.add("Calificación 1:\n"+nodo.calif1);
lista.add("Calificación 2:\n"+nodo.calif2);
lista.add("Calificación 3\n"+nodo.calif3);
promedio(nodo.calif1, nodo.calif2, nodo.calif3);
lista.add("Su promedio es:\n"+prom);
System.out.println( "¿Desea ingresar otro alumno?" );
System.out.println( "1.-Si\t 2.-No" );
op = leer.nextInt();
}
while(op != 2);
List lista2 = new ArrayList(lista);
Iterator it = lista2.iterator();
while (it.hasNext()){
System.out.println(it.next()+"");
}
}
private static double promedio(int calif1, int calif2, int calif3){
int suma = calif1 + calif2 + calif3;
prom = suma/3;
return prom;
}
}
P.D.: No es necesario hacer el NodoLista4, esas variables se pueden crear en el programa, pero me acostumbre a hacer eso…

