You are on page 1of 13

LISTA ENLAZADA DOBLEMENTE CIRCULAR

MAIN

package ledccoboy;

import javax.swing.JOptionPane;

public class LEDCCoboy {

public static void main(String[] args) {

int opc=0,elemento,op;

ListaDC Coboy = new ListaDC ();

do{

do{

try{

opc=Integer.parseInt(JOptionPane.showInputDialog(null,

"<html><font face='Century Gothic' size='3'>"+"1.-Agregar Nodo al


Inicio\n"+

"<html><font face='Century Gothic' size='3'>"+"2.-Agregar Nodo al


Final\n"+

"<html><font face='Century Gothic' size='3'>"+"3.-Mostrar Nodo desde


el Inicio\n"+

"<html><font face='Century Gothic' size='3'>"+"4.-Mostrar Nodo desde


el Final\n"+

"<html><font face='Century Gothic' size='3'>"+"5.-Eliminar al Inicio\n"+

"<html><font face='Century Gothic' size='3'>"+"6.-Eliminar al Final\n"+

"<html><font face='Century Gothic' size='3'>"+"7.-Eliminar elemento\n"+

"<html><font face='Century Gothic' size='3'>"+"8.-Buscar\n"+

"<html><font face='Century Gothic' size='3'>"+"9.-Salir","OPCIONES DE


MENU",1));

switch(opc){

case 1:

do{
try{

elemento=Integer.parseInt(JOptionPane.showInputDialog(null,"Agregar elemento
a la lista","AGREGAR NODO AL INICIO",3));

Coboy.agregarInicio(elemento);

break;

}catch(NumberFormatException e){

op = JOptionPane.showConfirmDialog(null,"DATO
INCORRECTO\n"+"¿DESEA VOLVER INTERTARLO?");

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

if(op == 2)

break;

}while(true);

break;

case 2:

do{

try{

elemento=Integer.parseInt(JOptionPane.showInputDialog(null,"Agregar elemento
a la lista","AGERGAR NODO AL FINAL",3));

Coboy.agregarFinal(elemento);

break;

}catch(NumberFormatException e){

op = JOptionPane.showConfirmDialog(null,"DATO
INCORRECTO\n"+"¿DESEA VOLVER INTERTARLO?");
if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

if(op == 2)

break;

}while(true);

break;

case 3:

op = JOptionPane.showConfirmDialog(null,"¿DESEA MOSTRAR LISTA


DEL INCIO AL FIN?");

if(op == 0)

Coboy.mostrarInicioFin();

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

break;

case 4:

op = JOptionPane.showConfirmDialog(null,"¿DESEA MOSTRAR LISTA


DEL INCIO AL FIN?");

if(op == 0)

Coboy.mostrarFinInico();

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;
}

break;

case 5:

op = JOptionPane.showConfirmDialog(null,"¿DESEA ELIMINAR EL
INCIO DE LA LSITA?");

if(op == 0)

Coboy.eliminarAlInicio();

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

break;

case 6:

op = JOptionPane.showConfirmDialog(null,"¿DESEA ELIMINAR EL
FINAL DE LA LSITA?");

if(op == 0)

Coboy.eliminarAlFinal();

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

break;

case 7:

do{

try{

elemento =
Integer.parseInt(JOptionPane.showInputDialog(null,"Ingrese elemento a
eliminar","ELIMINAR",0));

if(Coboy.buscar(elemento)){
op = JOptionPane.showConfirmDialog(null,"DESEA ELIMINAR
ELEMENTO");

if(op == 0){

Coboy.eliminarEspecifico(elemento);

JOptionPane.showMessageDialog(null,"Elemnto eliminado
exitosamente", "ELEMENTO ELIMINADO",0);

break;

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

else

JOptionPane.showMessageDialog(null,"EL ELEMENTO
SELECCIONADO NO EXISTE","NO EXISTE",0);

}catch(NumberFormatException e){

op = JOptionPane.showConfirmDialog(null,"DATO
INCORRECTO\n"+"¿DESEA VOLVER INTERTARLO?");

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

if(op == 2)

break;

}while(true);

break;
case 8:

do{

try{

elemento =
Integer.parseInt(JOptionPane.showInputDialog(null,"Ingrese elemento a
buscar","BUSCADOR",0));

if(Coboy.buscar(elemento))

JOptionPane.showMessageDialog(null,"EL ELEMENTO
SELECCIONADO SI EXISTE","BUSCADOR",-1);

else

JOptionPane.showMessageDialog(null,"EL ELEMENTO
SELECCIONADO NO EXISTE","BUSCADOR",0);

break;

}catch(NumberFormatException e){

op = JOptionPane.showConfirmDialog(null,"DATO
INCORRECTO\n"+"¿DESEA VOLVER INTERTARLO?");

if(op == 1){

JOptionPane.showMessageDialog(null," REGRESAR AL MENU


PRINCIPAL ");

break;

if(op == 2)

break;

}while(true);

break;

case 9:

JOptionPane.showMessageDialog(null,"HASTA LA PROXIMA\n"
+"VUELVA PRONTO","FIN DEL PROGRAMA",1);

System.exit(0);

break;
default:

JOptionPane.showMessageDialog(null,"LA OPCION INGRESADA\n" +"


ES INCORRECTA","",0);

}catch(NumberFormatException e){

if(e.getMessage().equals("null"))

System.exit(0);

else

JOptionPane.showMessageDialog(null,"OPCION INGRESADA ES
INCORECTA\n","OPCION INCORECTA",0);

}while(true);

}while(opc != 9);

}//main

LISTA

package ledccoboy;

import javax.swing.JOptionPane;

public class ListaDC {

private NodoDobleC inicio,fin;

public ListaDC(){

inicio=fin=null;

public boolean estaVacia(){

return inicio==null;
}

public void agregarInicio(int elem){

if(!estaVacia()){

inicio=new NodoDobleC(elem,inicio,fin) ;

inicio.siguiente.anterior=inicio;

fin.siguiente = inicio;

else{ //primer elemento

inicio=fin=new NodoDobleC(elem);

fin.siguiente= inicio; //enlaza al inicio;

inicio.anterior = fin; //eenlaza al fin

public void agregarFinal(int elem){

if(!estaVacia())

fin=new NodoDobleC(elem,inicio,fin);

inicio.anterior = fin;

fin.anterior.siguiente = fin;

else{

inicio=fin=new NodoDobleC(elem);

fin.siguiente= inicio; //enlaza al inicio;

inicio.anterior = fin; //eenlaza al fin

}
public void mostrarInicioFin (){ //incio al fin

if(!estaVacia()){

String data="";

NodoDobleC aux=inicio;

do{

data += "["+aux.dato+"]<=>";

//data=data+"["+aux.dato+"]<=>";

aux=aux.siguiente;

}while(aux != inicio);

JOptionPane.showMessageDialog(null,data,"Mostrar Datos del Inicio al


Final",-1);

else

JOptionPane.showMessageDialog(null,"NO HAY DATOS QUE MOSTRAR","NO


HAY DATOS",-1);

public void mostrarFinInico (){ //del fin al inicio

if(!estaVacia()){

String data="";

NodoDobleC aux=fin;

do{

data += "["+aux.dato+"]<=>";

//data=data+"["+aux.dato+"]<=>";

aux=aux.anterior;

}while(aux != fin);

JOptionPane.showMessageDialog(null,data,"Mostrar Datos del Final al


Inicio",-1);

else
JOptionPane.showMessageDialog(null,"NO HAY DATOS QUE MOSTRAR","NO
HAY DATOS",-1);

public void eliminarAlInicio(){

int elemento=inicio.dato;

if(inicio==fin)

inicio=fin=null;

else{

inicio=inicio.siguiente;

inicio.anterior=fin;//apunta a fin

fin.siguiente = inicio;

JOptionPane.showMessageDialog(null,"El elemento eliminado hacido :


"+elemento,"ELEMENTO ELIMINADO",0);

public void eliminarAlFinal(){

int elemento=fin.dato;

if(inicio==fin)

inicio=fin=null;

else{

fin = fin.anterior;

fin.siguiente = inicio;

inicio.anterior = fin;

JOptionPane.showMessageDialog(null,"El elemento eliminado hacido :


"+elemento,"ELEMENTO ELIMINADO",0);

}
public void eliminarEspecifico(int elem){

if(!estaVacia()){

NodoDobleC aux= inicio;

NodoDobleC ant= null;

while(ant!= fin){

if(aux.dato == elem){

if(ant == null){ //cuando se agrego un solo elemento en la lista

if(inicio == fin){

inicio = fin = null;

break;

else{

inicio = inicio.siguiente;

aux.siguiente = null;

inicio.anterior = fin;

fin.siguiente = inicio;

break;

else{ //referencia al ultimo elemento

if(aux == fin){ //el ultimo elemnto de la lista

ant.siguiente = null;

fin.siguiente = null;

inicio.anterior = null;

fin.anterior = null;

fin = ant;

fin.siguiente = inicio;

inicio.anterior = fin;

break;
}

else{

ant.siguiente = aux.siguiente;

aux.anterior = null;

aux.siguiente.anterior = ant;

break;

else{

ant = aux;

aux = aux.siguiente;

public boolean buscar (int elem ){

if(!estaVacia()){

NodoDobleC aux = inicio;

do{

if(aux.dato==elem)

return true;

aux = aux.siguiente;

}while(aux!=inicio);

return false;

}
NODO

package ledccoboy;

public class NodoDobleC {

int dato;

NodoDobleC siguiente, anterior;

public NodoDobleC(int elem,NodoDobleC sig,NodoDobleC ant){

dato = elem;

siguiente = sig;

anterior = ant;

public NodoDobleC(int elem){

dato = elem;

You might also like