Programacion Java Ejemplos COLAS
6.lilithPráctica o problema22 de Junio de 2020
10.684 Palabras (43 Páginas)237 Visitas
CÓDIGOS DE PROGRAMACIÓN EN NETBEANS
EJEMPLOS DE COLAS
EJEMPLO 1
Código Fuente
Insertar
public class Insertar {
static class Nodo {
int dato;
Nodo sig;
}
public static void main(String[] args) {
int n;
Nodo top = null;
Nodo ultimo = null;
System.out.println("Ingrese el número de elementos de la cola");
Scanner leer= new Scanner(System.in);
n= leer.nextInt();
System.out.println("COLA");
for (int i = 1; i <= n; i++) {
Nodo temp = new Nodo();
temp.dato = i;
temp.sig = null;
if (top == null) {
top = temp;
} else {
ultimo.sig = temp;
}
ultimo = temp;
}
Nodo temp = top;
while (temp != null) {
System.out.println(temp.dato);
temp = temp.sig;
}
}
}
MODIFICAR
public class Modificar {
static class Nodo {
int dato;
Nodo sig;
}
public static void main(String[] args) {
int n;
int buscar;
int modificar;
Nodo top = null;
Nodo ultimo = null;
Nodo colat = null;
Nodo ultimot = null;
Scanner leer= new Scanner(System.in);
System.out.println("Ingrese el número de elementos de la cola");
n= leer.nextInt();
System.out.println("COLA");
for (int i = 1; i <= n; i++) {
Nodo temp = new Nodo();
temp.dato = i;
temp.sig = null;
if (top == null) {
top = temp;
} else {
ultimo.sig = temp;
}
ultimo = temp;
System.out.println(temp.dato);
}
Nodo temp = top;
System.out.println("\n Ingrese el numero que desea modificar");
buscar = leer.nextInt();
while (temp.dato != buscar) {
Nodo temp1 = new Nodo();
temp1.dato = temp.dato;
temp1.sig = null;
if (colat == null) {
colat = temp1;
} else {
ultimot.sig = temp1;
}
ultimot = temp1;
top = top.sig;
temp = top;
}
System.out.println("Ingrese el nuevo dato");
modificar = leer.nextInt();
top.dato = modificar;
ultimot.sig = top;
top = colat;
colat = null;
temp = top;
System.out.println("\n COLA ACTUAL");
while (temp != null) {
System.out.println(temp.dato);
temp = temp.sig;
}
}
}
ELIMINAR
public class Eliminar {
static class Nodo {
int dato;
Nodo sig;
}
public static void main(String[] args) {
int n;
int buscar;
Nodo top = null;
Nodo ultimo = null;
Nodo colat = null;
Nodo ultimot = null;
Scanner leer= new Scanner(System.in);
System.out.println("Ingrese el número de elementos de la cola");
n= leer.nextInt();
System.out.println("COLA");
for (int i = 1; i <= n; i++) {
Nodo temp = new Nodo();
temp.dato = i;
temp.sig = null;
if (top == null) {
top = temp;
} else {
ultimo.sig = temp;
}
ultimo = temp;
System.out.println(temp.dato);
}
Nodo temp = top;
System.out.println("\n Ingrese el numero que desea eliminar");
buscar = leer.nextInt();
while (temp.dato != buscar) {
Nodo temp1 = new Nodo();
temp1.dato = temp.dato;
temp1.sig = null;
if (colat == null) {
colat = temp1;
} else {
ultimot.sig = temp1;
}
ultimot = temp1;
top = top.sig;
temp = top;
}
top = top.sig;
ultimot.sig = top;
top = colat;
colat = null;
temp = top;
System.out.println("\n Nueva cola");
while (temp != null) {
System.out.println(temp.dato);
temp = temp.sig;
}
}
}
*************************************************************
EJEMPLO 2
Código fuente
> Clase nodo
public class Nodo {
int dato;
Nodo sig;
}
> Clase métodos
public class Metodos {
String n, m, eliminar, modificar, buscar;
Nodo top = null;
Nodo ultimo = top;
Nodo colat = null;
Scanner leer = new Scanner(System.in);
public void insertarCola() {
Metodos obj = new Metodos();
System.out.println("Ingrese cuantos numeros va a ingresar en la Cola: ");
n = leer.next();
if (obj.isNumeric(n)) {
System.out.println("Generar multiplos de: ");
m = leer.next();
if (obj.isNumeric(m)) {
int num1 = Integer.parseInt(n);
int num2 = Integer.parseInt(m);
for (int i = 1; i <= num1; i++) {
Nodo temp = new Nodo();
temp.dato = i * num2;
temp.sig = null;
if (top == null) {
top = temp;
} else {
ultimo.sig = temp;
}
ultimo = temp;
}
Nodo temp = top;
} else {
System.out.println("Ingrese solo números");
}
} else {
System.out.println("Ingrese solo números");
}
}
public void imprimirCola() {
if (top == null) {
System.out.println("LA COLA NO CONTIENE ELMENTOS");
System.out.println("");
} else {
Nodo temp = top;
while (temp != null) {
System.out.print(" " + temp.dato);
temp = temp.sig;
}
System.out.println();
System.out.println();
}
}
public void eliminarElemento() {
Metodos obj = new Metodos();
if (top == null) {
System.out.println("NO EXISTEN ELEMENTOS PARA ELIMINAR");
System.out.println("");
} else {
System.out.println(top.dato);
System.out.println("Ingrese el numero que desea eliminar: ");
eliminar = leer.next();
if (obj.isNumeric(eliminar)) {
Nodo temp = top;
Nodo ultimot = null;
int eliminarAux = Integer.parseInt(eliminar);
if (eliminarAux == top.dato) {
top = top.sig;
} else {
while (temp.dato != eliminarAux) {
Nodo temp1 = new Nodo();
temp1.dato =
...