Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/04/2008, 21:42
jalex16
 
Fecha de Ingreso: octubre-2006
Mensajes: 562
Antigüedad: 18 años, 5 meses
Puntos: 12
se repite resultado hasta nunca terminar

hola, estoy haciendo un applet que se supone debe leer un arreglo ordenado, y al dar clic en Buscar, debe indicar si existe el elemento o no en dicho arreglo, pero cuando hago esto, funciona, pero el resultado se repite infinitamente.
este es el codigo

Código:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Lista extends Applet implements
ActionListener {
  Label lbl1=new Label("Dato:");
  TextField Dato=new TextField();
  
  Button Insertar=new Button("Insertar");
  Button Buscar=new Button("Buscar");
  
  Label lbl2=new Label("Elementos de la Lista:");
  TextArea  Lista=new TextArea("");
  
  Button Mostrar=new Button("Mostrar");
  Button Limpiar=new Button("Limpiar");
  Button Eliminar=new Button("Eliminar");   
   
  // inicializando estructura de datos
   int D[]=new int[20];
   int S[]=new int[20];
   int L=-1;
   int Dispo=0;
   
   //fin de inicializacion de estructura de datos
   
  public void init(){
   setLayout(null);
   setBackground(java.awt.Color.white);
   resize(350,250);
   lbl1.setBounds(20,15,30,20);
   Dato.setBounds(55,15,120,20);
   Insertar.setBounds(20,60,80,20);
   Buscar.setBounds(110,60,80,20);
   Eliminar.setBounds(200,60,80,20);
   lbl2.setBounds(50,100,140,20);
   Lista.setBounds(50,120,190,60);
   Mostrar.setBounds(20,200,80,20);
   Limpiar.setBounds(110,200,80,20);
   Insertar.addActionListener(this);
   Buscar.addActionListener(this);
   Mostrar.addActionListener(this);
   Limpiar.addActionListener(this);
   add(lbl1);add(Dato);add(Insertar);add(Eliminar);add(Buscar);add(lbl2);
   add(Lista);add(Mostrar);add(Limpiar);
   //incializa arreglo S de elementos disponibles
   for(int i=0;i<19;i++)
       S[i]=i+1;
       S[19]=-1; 
  }
  public void actionPerformed(ActionEvent ac) {
   String str=ac.getActionCommand();
   int x;
   x=Integer.parseInt(Dato.getText());
   if(str.equals("Insertar")) Insertar(x);
   if(str.equals("Eliminar")) Eliminar(x); 
   if(str.equals("Buscar")) Buscar(x);
   if(str.equals("Mostrar")) Mostrar();
   if(str.equals("Limpiar")) Limpiar(); 
  }
  void Insertar(int y){
    int p=0,q=0,r=0;
    if(L==-1){
     r=Dispo;
     Dispo=S[Dispo];
     L=r;
     D[r]=y;
     S[r]=-1;
     return;
    }    
    else {
     if(Dispo !=-1){
      p=L;
      q=-1;}
      while(p!=-1){
       if(y>D[p])
        {
         q=p;
         p=S[p];
         }
       else {
        if(y==D[p]) return;
         else if(q!=-1){
             r=Dispo;
             Dispo=S[Dispo];
             D[r]=y;
             S[q]=r;
             S[r]=p;
             return;   
           }
           else
           {
            r=Dispo;
            Dispo=S[Dispo];
            D[r]=y;
            S[r]=L;
            L=r;
            return;
           }
       }
      }
       
      if(p==-1){
       r=Dispo;
       Dispo=S[Dispo];
       S[q]=r;
       S[r]=-1;
       D[r]=y;
       return; 
      } 
    }
  }
  void Buscar (int y){
   p=L;
   if (p!=-1)
    if (y>D[p])
     p=S[p];
    else
     if (y==D[p]){
      Lista.append("Elemento existe");
      return;
     }
     else{
      Lista.append("Elemento no se encuentra");
      return;
     }
   if (p==-1{){
    Lista.append("Elemento no se encuentra");
    return;
   }
  }
  
  void Eliminar(int y){
   int p;
   p=L;
   int n=D.length;
   while(p!=-1){
   if(y>D[p])
    p=S[p];
   else if(y==D[p])
    for(int i=p;i<n;i++)
    {
     D[i]=D[i+1];
    }
    else Lista.append("Elemento no se encuentra");
   }
   if(p==-1)Lista.append("Lista vacía");
    
  }
  void Mostrar(){
   int p;
   p=L;
   Lista.setText("");
   while(p!=-1){
    Lista.append(D[p]+", ");
    p=S[p]; 
   }
  }
  void Limpiar(){
   Lista.setText("");
   Dato.setText("");
   Dato.requestFocus(); 
  }
}