2012-11-25 18 views
1

我有疑問:在雙鏈表中按字母順序排列

我創建了以下程序,允許您介紹有關自行車的不同數據。 我想要做的是,當你介紹了自行車的名字,然後你想讀的介紹了數據,程序將顯示你的自行車名稱的字母順序

這是我的代碼

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,然後是貝諾託作爲名字,當我按#3顯示數據時,我必須按名稱按字母順序查看第一個Benoto,然後是BMX等等。

在此先感謝!

+0

我會說你必須選擇一個合適的插入點,以便保持字母順序。 –

+0

你的意思是什麼?你將如何創建一個方法以按字母順序顯示名稱? :/ Thx再次! –

+0

那麼,你總是可以將元素提取到數組中並對數組進行排序。或者你可以編寫你的鏈表算法,這樣當你插入一個元素時,它會「遍歷」列表並找到第一個元素的鍵大於要插入的元素,然後在找到的元素之前插入。 –

回答

0

你應該改變你的代碼,消除ListaLigada.java的部件,更換與任何ArrayList<NodoL>或用TreeMap<NodoL>,或者如果你也希望有一個LinkedList<NodoL>.

有沒有必要重新實現藏品,其中已經存在在Java中

+0

有沒有其他的方法來實現一個方法,而不是doint你說什麼? :/ 否則,我將不得不改變更多的東西 –

+0

有沒有很多方法。使用java集合將會更便宜,您也可以編寫與現有insert()兼容的類,並在該類內部有ArrayList或Map,它可以進行存儲和排序。然後你的insert()會在內部調用list.add()或類似的東西。 – AlexWien

+0

你可以給我這個班的模板嗎?所以我可以使用它。 在這一刻我不知道如何去做:S –