Manybadilla |
14-abr-2010 13:04 |
[JAVA] Estructura de datos, Colas y Pilas, a pie...!
Buenas compañeros y colegas, expondre unas clases sencillas de colas y pilas en java, esto se desarrolla casi siempre en algun curso de estructura de datos y por la web no hay mucho ejemplo de dichas cosas que son básica a la hora de desarrollar algoritmos y estructuras de datos.
Primero la clase Cola
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Cola;
import javax.swing.JOptionPane;
/**
*
* @author Manybadilla
*/
public class Cola {
Nodo front;
Nodo back;
public void enqueue(Persona persona) {
Nodo nuevo = new Nodo(persona);
if (isEmpty()) {
front = nuevo;
back = nuevo;
} else {
back.setTop(nuevo);
back = nuevo;
}
}
public Nodo getBack() {
return back;
}
public Nodo getFront() {
return front;
}
public void denqueue() {
if (isEmpty()) {
} else {
front = front.getTop();
}
}
public String printStack() {
Nodo actual = front;
String mensaje = "";
if (isEmpty()) {
return "Esta Vacia";
} else {
while (actual != null) {
mensaje += actual.getPersona().toString() + "\n";
actual = actual.getTop();
}
}
return mensaje;
}
public boolean isEmpty() {
if (front == null) {
return true;
} else {
return false;
}
}
}
Segundo la clase Nodo
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Cola;
/**
*
* @author Manybadilla
*/
public class Nodo {
private Nodo top;
private Persona persona;
public Nodo() {
}
public Nodo(Persona persona) {
this.persona = persona;
}
public Persona getPersona() {
return persona;
}
public Nodo getTop() {
return top;
}
public void setTop(Nodo top) {
this.top = top;
}
}
Tercero la clase Persona (Pues manejaremos objetos de tipo persona, con 2 atributos nombre y apellido)
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Cola;
/**
*
* @author Manybadilla
*/
public class Persona {
private String nombre;
private String apellido;
public Persona(String nombre, String apellido) {
this.nombre = nombre;
this.apellido = apellido;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@Override
public String toString(){
return "Nombre: "+nombre+" "+apellido;
}
}
Cuarto una interfaz para enterder visual y menos lógicamente
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Ventana.java
*
* Created on 13/04/2010, 09:00:48 AM
*/
package Cola;
import javax.swing.JOptionPane;
/**
*
* @author Manybadilla
*/
public class Ventana extends javax.swing.JFrame {
Cola cola = new Cola();
String string = new String();
/** Creates new form Ventana */
public Ventana() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("<<");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Cola");
jButton2.setText(">>");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Front");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel2.setText("Top");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3))
.addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
Persona persona = new Persona(JOptionPane.showInputDialog(null, "Ingrese el nombre:"), JOptionPane.showInputDialog(null, "Ingrese el Apellido:"));
cola.enqueue(persona);
string = persona.toString() + "\n" + string;
jTextArea1.setText(string.toString());
// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
string = string.replaceFirst(cola.getFront().getPersona().toString(), "");
jTextArea1.setText(string);
cola.denqueue();
// TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
}//GEN-LAST:event_jButton3ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ventana().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
Esto corresponde a Cola (Que se basan en la lista).
Para descargar todo el proyecto: http://www.***************/?d=1ROLLOLU
__________________________________________________ __________________________________________________ ______________________________
Ahora veremos una Pila:
Primera clase Pila (Practicamente posee toda la lógica)
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Pilas;
import javax.swing.JOptionPane;
/**
*
* @author Manybadilla
*/
public class Pila {
Nodo primero;
public void push(Persona persona) {
Nodo nuevo = new Nodo(persona);
if (isEmpty()) {
primero = nuevo;
} else {
nuevo.setTop(primero);
primero = nuevo;
}
}
public void pop() {
if (isEmpty()) {
JOptionPane.showMessageDialog(null, "La Pila esta vacÃ*a");
} else {
primero = primero.getTop();
}
}
public String top() {
if (primero == null) {
return "Esta vacÃ*a";
} else {
return primero.getPersona().toString();
}
}
public boolean isEmpty() {
if (primero == null) {
return true;
} else {
return false;
}
}
public String printStack() {
Nodo actual = primero;
String mensaje = "";
if (isEmpty()) {
return "Esta VacÃ*a";
} else {
while (actual != null) {
mensaje += actual.getPersona().toString() + "\n";
actual = actual.getTop();
}
}
return mensaje;
}
}
Segunda clase Persona (La misma de la cola, es solo para instanciar objetos de tipo persona)
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Pilas;
/**
*
* @author Manybadilla
*/
public class Persona {
private String nombre;
private String apellido;
public Persona(String nombre, String apellido) {
this.nombre = nombre;
this.apellido = apellido;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@Override
public String toString(){
return "Nombre: "+nombre+" "+apellido;
}
}
La clase Nodo (La misma)
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Pilas;
/**
*
* @author Manybadilla
*/
public class Nodo {
private Nodo top;
private Persona persona;
public Nodo() {
}
public Nodo(Persona persona) {
this.persona = persona;
}
public Persona getPersona() {
return persona;
}
public Nodo getTop() {
return top;
}
public void setTop(Nodo top) {
this.top = top;
}
}
Cuarto la interfaz
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Ventana.java
*
* Created on 13/04/2010, 09:00:48 AM
*/
package Pilas;
import javax.swing.JOptionPane;
/**
*
* @author Manybadilla
*/
public class Ventana extends javax.swing.JFrame {
static Pila pila = new Pila();
/** Creates new form Ventana */
public Ventana() {
initComponents();
jTextArea1.setText(pila.printStack());
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("<<");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Pila");
jButton2.setText(">>");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Top");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel2.setText("Top");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addContainerGap(374, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
.addContainerGap()))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3))
.addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
Persona persona = new Persona(JOptionPane.showInputDialog(null, "Ingrese el nombre:"), JOptionPane.showInputDialog(null, "Ingrese el Apellido:"));
pila.push(persona);
jTextArea1.setText(pila.printStack());
// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
pila.pop();
jTextArea1.setText(pila.printStack());// TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
jTextField1.setText(pila.top());
// TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ventana().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
Descargar todo el proyecto de una Pila: http://www.***************/?d=7JMIDERV
Esto es todo compañeros, se que esto será de gran ayuda y menos dolores de cabeza.
Saludos.:bocina:
|