链接列表(搜索和插入) [英] Linkedlist (search and insert)

查看:78
本文介绍了链接列表(搜索和插入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello guys,

I have my methods to search, delete Start, delete End, Add Start, Add to End, but I have not been able to develop the methods to:

Search and Delete: the item searched
Insert element: Insert element in the position indicated by the user.

I hope you can help me with this methods, please.

Thanks in advance.

DS





我的尝试:





What I have tried:

I share my methods and class

<pre lang="java">
//Node class
package asesorialistas;
/*@author Daniel Solis*/
public class Nodo{
    
public int Dato;
public Nodo Sig;//Puntero Enlcae recursivo
//Enlaza los diferentes nodos


//Constructor inicializar nodos
public Nodo(int d){
//Para crear un nodo final
this.Dato=d;  
}
//Constructor para insertar al inicio
public Nodo(int d, Nodo n){
this.Dato=d;
this.Sig=n;
    }
}

//List Methods
package asesorialistas;
/*@author Daniel Solis*/
public class Lista {
//Creando punteros
protected Nodo Inicio, Fin; //Punteros para saber donde inica y donde termina
//Donde esta el Inicio y Fin
public Lista(){
Inicio=null;
Fin=null;
    }
/*----------------------------------------------------------------------------*/
//Metodo para agregar al incio de la lista
public void AgregarInicio(int Elemento){
//Creando el nodo
Inicio = new Nodo(Elemento,Inicio);//Creando el Nodo
if(Fin==null){
    Fin=Inicio;
    }
}
/*----------------------------------------------------------------------------*/
//Metodo para agregar al final
public void AgrearFin(int Elemento){
    if (!EstaVacia()) {
      Fin.Sig=new Nodo(Elemento);
      Fin=Fin.Sig;   
    }else{
        Inicio=Fin=new Nodo(Elemento);
    }
}
/*----------------------------------------------------------------------------*/
//Metodo para mostrar la lista
public void Mostrar(){
Nodo recorrer=Inicio;
while (recorrer!=null){
    System.out.print("["+recorrer.Dato+"]");
    recorrer=recorrer.Sig;
    }
}
/*----------------------------------------------------------------------------*/
//Metodo esta la lista Vacia
public boolean EstaVacia() {
    return Inicio==null;  
}
/*----------------------------------------------------------------------------*/
public int BorrarInicio(){
        //creando el nodo
        int Elemento = Inicio.Dato;
        if(Inicio==Fin){
            Inicio = Fin = null;
        }
        else{
            Inicio=Inicio.Sig;
        }
        return Elemento;
}
/*----------------------------------------------------------------------------*/
public int BorrarFinal(){
        int Elemento = Fin.Dato;
        if(Inicio==Fin){
            Inicio = Fin = null;
        }else{
            Nodo temporal = Inicio;
            while(temporal.Sig != Fin){
                temporal = temporal.Sig;
            }
            Fin = temporal;
            Fin.Sig = null;
        }
        return Elemento;
}
/*----------------------------------------------------------------------------*/
public boolean BuscarElemento (int elemento){
    Nodo temporal=Inicio;
    while (temporal!=null&& temporal.Dato!=elemento)
    {
    temporal=temporal.Sig;
    }
    return temporal!=null;
    }

public boolean BuscarElementoEsp (int elemento){
    Nodo temporal=Inicio;
    while (temporal!=null&& temporal.Dato!=elemento)
    {
    temporal=temporal.Sig;
    }
    return temporal!=null;
    }
}

//MAIN
package asesorialistas;
import java.awt.HeadlessException;
import javax.swing.JOptionPane;
/*@author Dany*/
public class Principal {
    public static void main(String[] args) {
        //crear instancias de las clases
        Lista Listita = new Lista();
        //menu do while
        int opcion = 0;
        int Elemento; //valor a la lista (para insertar)
        
        do {
            try {
                opcion = Integer.parseInt(JOptionPane.showInputDialog(null, "***  MENU DE OPCIONES  ***\n"
                        + "1.- Agregar un Elemento al Inicio \n"
                        + "2.- Agregar un Elemento al Final \n"
                        + "3.- Mostrar los Elementos de las listas \n"
                        + "4.- Checar si la lista esta vacia \n"
                        + "5.- Borrar Elemento al inicio \n"
                        + "6.- Borrar Elemento al final \n"
                        + "7.- Buscar Elemento \n"
                        + "8.- Buscar Elemento y eliminar elemento \n"
                        + "9.- Salir \n"));

                switch(opcion){
                    
                    case 1:
                        try {
                            Elemento = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingresa el Elemento "
                                    + "al inicio"));
                            Listita.AgregarInicio(Elemento);
                        } catch (NumberFormatException e) {
                            JOptionPane.showMessageDialog(null,"Error"+e.getMessage());
                        }
                        break;
                        
                    case 2:
                                                try {
                           Elemento = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingresa el Elemento "
                                    + "al final"));
                            Listita.AgrearFin(Elemento); 
                        } catch (NumberFormatException e) {
                            JOptionPane.showMessageDialog(null,"Error"+e.getMessage()); 
                        }
                        break;
  
                    case 3:
                        Listita.Mostrar();
                        System.out.println("");
                        break;
                       
                    case 4:
                        Listita.EstaVacia();
                        break;
                        
                    case 5:
                        try {
                            Elemento = Listita.BorrarInicio();
                            JOptionPane.showMessageDialog(null, "El Elemento eliminado es: "
                                    +Elemento,"Eliminando nodo de inicio",
                                    JOptionPane.INFORMATION_MESSAGE);
                            
                        } catch (NumberFormatException e) {
                            JOptionPane.showMessageDialog(null,"Error"+e.getMessage());
                        }
                        break;
                        
                     case 6:
                        try {
                            Elemento = Listita.BorrarFinal();
                            JOptionPane.showMessageDialog(null, "El Elemento eliminado es: "
                                    +Elemento,"Eliminando nodo de fin",
                                    JOptionPane.INFORMATION_MESSAGE);
                            
                        } catch (NumberFormatException e) {
                            JOptionPane.showMessageDialog(null,"Error"+e.getMessage());
                        }
                        break;
                        
                        case 7:
                            Elemento=Integer.parseInt(JOptionPane.showInputDialog(null,"Ingresa el "+" elemento a buscar...","Buscando nodos en la lista",
                                    JOptionPane.INFORMATION_MESSAGE));
                            if (Listita.BuscarElemento(Elemento)==true) {
                            JOptionPane.showMessageDialog(null, "El elemento " + Elemento + " si esta en la lista",
                                        "nodo encontrado",JOptionPane.INFORMATION_MESSAGE);  
                            }else{JOptionPane.showMessageDialog(null, "El elemento " + Elemento + " no esta en la lista",
                                        "nodo no encintrado",JOptionPane.INFORMATION_MESSAGE);       
                            }   
                        break;
                        
                        case 8:
                            Elemento=Integer.parseInt(JOptionPane.showInputDialog(null,"Ingresa el "+" elemento a buscar...","Buscando nodos en la lista",
                                    JOptionPane.INFORMATION_MESSAGE));
                            if (Listita.BuscarElemento(Elemento)==true) {
                            int resp = JOptionPane.showConfirmDialog(null, "El elemento " + Elemento + " si esta en la lista quieres eliminarlo");
                                    if (JOptionPane.OK_OPTION == resp){
                                            System.out.println("Eliminar registro");
                                            }else{System.out.println("No Eliminar");
                                        }
                            }else{JOptionPane.showMessageDialog(null, "El elemento " + Elemento + " no esta en la lista",
                                        "nodo no encintrado",JOptionPane.INFORMATION_MESSAGE);       
                            }   
                        break;      
                }
            } catch (HeadlessException | NumberFormatException e) {
                JOptionPane.showMessageDialog(null,"Error"+ e.getMessage());
            }
        } while (opcion != 9);
        
    }
}





案例八,用于搜索和删除。我还没完成。



Case eight, is for search and delete. I do not finish it yet.

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!你有搜索的代码,你有删除的代码:把它们组合在一起!



如果你遇到一个特定的问题,那么请问一下我们会做什么我们最好的帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! You have the code for searching, you have the code for deleting: put them together!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于链接列表(搜索和插入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆