나는 의심 할 여지가있다 :이중 고리로 된 목록으로 알파벳 순서로 정렬
나는 자전거에 관한 다른 데이터를 소개 할 수있는 다음과 같은 프로그램을 만들었다. 는 내가하고 싶은 것은 당신이 자전거의 이름을 소개 한 다음 도입 된 데이터를 읽고 자 할 때 프로그램이 자전거 '순으로 이름
을 보여주고 있다는 점이다이 내 코드
입니다NodoL.java
package listaligadoble;
public class NodoL {
String nombre;
String estilo;
int rodada;
NodoL sig;
NodoL ante;
public NodoL()
{ nombre = "Bennoto";
estilo = "Montana";
rodada = 26;
sig = null;
ante = null;
}
public NodoL (String n,String d,int t)
{ nombre = n;
estilo = d;
rodada = t;
sig = null;
ante = null;
}
public String datos()
{return "Nombre: " + nombre + " Estilo: " + estilo + " Rodada: "+rodada;
}
}
ListaLigadaD.java
package listaligadoble;
import javax.swing.*;
public class ListaLigadaD
{ NodoL inicio;
public ListaLigadaD()
{inicio=null;
}
public void insertarIncio(String n, String d, int t)
{ NodoL ap = new NodoL (n,d,t);
if (inicio==null)
{ap.sig=inicio;
inicio=ap;
}
else {inicio.ante=ap;
ap.sig=inicio;
inicio=ap;
}
}
public void insertarFinal(String n, String d, int t)
{ NodoL ap = new NodoL (n,d,t);
if (inicio == null)
{ ap.sig=inicio;
inicio=ap;
}
else { NodoL aux = new NodoL();
NodoL ultimo = new NodoL();
aux=inicio;
while (aux != null)
{ if (aux.sig == null)
{ ultimo=aux;
aux=aux.sig;
}
else aux=aux.sig;
}
ultimo.sig=ap;
ap.ante = ultimo;
//ap.ante=ultimo;
//ap.sig=null;
}
}
public void recorrer()
{if (inicio == null)
System.out.println("Lista vacía");
else
{NodoL aux;
aux=inicio;
while(aux!=null)
{System.out.println(aux.datos()+"\n");
aux=aux.sig;
}
}
}
public void borrarRodada(int dato)
{NodoL aux=null;
NodoL posicion=null;
//aux=inicio;
boolean flag=true;
if (inicio==null)
{System.out.println("Lista vacía");
return;
}
else{aux=inicio;;
}
if(aux.rodada == dato)
{inicio=aux.sig;
System.out.println("Dato sacado:"+aux.datos());
}
else {while (aux!=null && flag == true)
{if(aux.rodada == dato)
{ posicion = aux;
flag=false;
}
else aux=aux.sig;
}
}
if (aux==null)
System.out.println("Dato no encontrado");
else{
try {System.out.println("Dato sacado:"+posicion.datos());
//posicion.sig=posicion.sig.sig;
posicion.ante.sig=posicion.sig;
posicion.sig.ante=posicion.ante;
}
catch(Exception ex){}
}
}
public void borrarNombre(String dato)
{NodoL aux=null;
NodoL posicion=null;
//aux=inicio;
boolean flag=true;
if (inicio==null)
{System.out.println("Lista vacía");
return;
}
else{aux=inicio;;
}
if(aux.nombre.equalsIgnoreCase(dato))
{inicio=aux.sig;
System.out.println("Dato sacado:"+aux.datos());
}
else {while (aux!=null && flag==true)
{if(aux.nombre.equalsIgnoreCase(dato))
{posicion=aux;
flag=false;
}
else aux=aux.sig;
}
}
if (aux==null)
System.out.println("Dato no encontrado");
else{
try {System.out.println("Dato sacado:"+posicion.datos());
//posicion.sig=posicion.sig.sig;
posicion.ante.sig=posicion.sig;
posicion.sig.ante=posicion.ante;
}
catch(Exception ex){}
}
}
public void menu()
{System.out.println("Opción 1: Insertar inicio");
System.out.println("Opción 2: Insertar final");
System.out.println("Opción 3: Recorrer");
System.out.println("Opción 4: Eliminar por nombre");
System.out.println("Opción 5: Eliminar por rodada");
System.out.println("Opción 6: Salir");
System.out.println("Elija usted una opción");
}
public static void main(String[] args) {
ListaLigadaD alfa=new ListaLigadaD();
String n,d;
int t;
int opcion=0;
boolean control=true;
do{alfa.menu();
opcion=Integer.parseInt(JOptionPane.showInputDialog("Escriba la opción"));
switch (opcion)
{case 1:n=JOptionPane.showInputDialog("Introduzca el nombre:");
d=JOptionPane.showInputDialog("Introduzca el estilo:");
t=Integer.parseInt(JOptionPane.showInputDialog("Introduzca el rodada:"));
alfa.insertarIncio(n, d,t);
break;
case 2:n=JOptionPane.showInputDialog("Introduzca el nombre:");
d=JOptionPane.showInputDialog("Introduzca el estilo:");
t=Integer.parseInt(JOptionPane.showInputDialog("Introduzca el rodada:"));
alfa.insertarFinal(n, d, t);
break;
case 3:alfa.recorrer();
break;
case 4:n=JOptionPane.showInputDialog("Introduzca el nombre:");
alfa.borrarNombre(n);
break;
case 5:t=Integer.parseInt(JOptionPane.showInputDialog("Introduzca el rodada:"));
alfa.borrarRodada(t);
break;
case 6: control=false;
break;
default:System.out.println("Opción no válida, intente otra vez");
break;
}
}
while (control==true) ;
}
}
그래서 목록의 처음에 노드를 삽입하는 옵션 # 1을 선택한다고 가정 해 봅니다. 그래서이 프로그램은 이름, 스타일 및 더 많은 것들을 소개하라고 요구합니다. noe # 2를 누르고 목록 끝에 노드를 삽입하기로 결정한 경우 프로그램은 위와 동일한 정보를 묻습니다.
Fianlly을 나는 # 3 내가 alphabeticall 순서를하지 않고 그냥 전에 도입 자전거 두 종류를 볼 수 있습니다 누르면 ...
내 질문은 다음이다 : 나는 자전거의 이름을 주문 할 수있는 방법 처음에는 BMX를 소개하고 그 다음 Benotto를 이름으로 사용하면 # 3을 눌러 데이터를 표시 할 때 첫 번째 Benoto와 BMX 등을 이름순으로 알파벳순으로 표시해야합니다.
미리 감사드립니다.
나는 알파벳 순서가 유지되도록 적절한 삽입 지점을 선택해야한다고 말하고 싶습니다. –
그게 무슨 뜻입니까? 알파벳순으로 이름을 표시하기 위해 메소드를 어떻게 만들겠습니까? :/ 다시 Thx! –
글쎄, 당신은 항상 요소를 배열로 추출하고 배열을 정렬 할 수 있습니다. 또는 링크 된 목록 알고리즘을 작성하여 요소를 삽입 할 때 목록을 "걷고"삽입 된 것보다 큰 키를 가진 첫 번째 요소를 찾은 다음 해당 발견 된 요소 앞에 삽입 할 수 있습니다. –