Java - Problema listas enlazadas Java

 
Vista:

Problema listas enlazadas Java

Publicado por karen (1 intervención) el 01/03/2015 23:18:23
Hola, tengo que hacer un programa que calcule la media y la desviacion estandar en JAVA(estoy usando netbeans) por medio de listas enlazadas, y no se como hacerlo, ya tengo el codigo de las listas (para llenarlas y mostrarlas) ahora solo me falta el metodo matematico, si pudieran ayudarme.
CLASE NODO
1
2
3
4
5
6
7
8
9
10
11
public class Nodo {
    int info;
    Nodo sig;
 
    Nodo(int dato)
    {
        info=dato;
        sig=null;
 
    }
}

CLASE LISTA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Lista {
 
    Nodo cabeza;
 
    Lista()
    {
        cabeza=null;
    }
 
     public boolean listaVacia()
     {
        return (cabeza==null)? true: false;
    }
 
    public void insFrente(int dato)
    {
        Nodo nuevo= new Nodo(dato);
 
        if(listaVacia())
        {
            cabeza=nuevo;
 
        }
        else
        {
            nuevo.sig=cabeza;
            cabeza=nuevo;
        }
 
    }
 
     public String recorre()
       {
           String s="Lista\n";
           Nodo aux=cabeza;
 
           while(aux!=null)
           {
               s+=aux.info + "\n";
               aux=aux.sig;
 
           }
 
       return s;
       }


Y todo esta con interfaz grafica, que contiene un boton para INGRESAR DATOS, uno para MOSTRAR LOS DATOS EN UN TEXTAREA y un boton que mostrara el resultado/calcular la media y la desviacion.

este es el codigo de los 2 botones que ya tengo listos
1
2
3
4
5
6
7
8
9
private void BtnguardarActionPerformed(java.awt.event.ActionEvent evt) {
        int dato=Integer.parseInt(Txtdato.getText());
       numeros.insFrente(dato);
    }
 
    private void BtnmostrarActionPerformed(java.awt.event.ActionEvent evt) {
       String s=numeros.recorre();
        Tamostrar.setText(s);
    }


les agradeceria mucho la ayuda
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Problema listas enlazadas Java

Publicado por Andrés (340 intervenciones) el 06/03/2015 17:33:11
class Node {

private Integer data;
private Node next;

public Node() {this(null, null);}

public Node(Integer data) {this(data,null);}

public Node(Integer data, Node next) {this.data = data; this.next = next;}

public Node(Node node) {

if(null == node) {
throw new IllegalArgumentException("Are you crazy?");
}

this.data = node.getData();
this.next = node.getNext();

}

public int getData() {return this.data;}
public void setData(int data) {this.data=data;}
public Node getNext() {return this.next;}
public void setNext(Node next) {this.next=next;}

public String toString() {

StringBuilder sf = new StringBuilder("Node[data:");
sf.append(this.data);
sf.append("]");

return sf.toString();

}

}

class MyList {

private Node head;

public boolean isEmpty() {return null == this.head;}

public Node getHead() {

Node node = null;

if(!this.isEmpty()) {

node = new Node(this.head);

}

return node;

}

public void addFirst(Node node) {

if(null == node) {
throw new IllegalArgumentException("Are you crazy?");
}

if(this.isEmpty()) {

this.head = node;

}else {

node.setNext(this.head);
this.head = node;
}

}

public Node getTail() {

Node aux = this.head;

while(null != aux.getNext()) {

aux = aux.getNext();


}

return aux;

}

public void add(Node node) {

if(null == node) {
throw new IllegalArgumentException("Are you crazy?");
}

if(this.isEmpty()) {

this.addFirst(node);

}else {

Node tail = this.getTail();
tail.setNext(node);

}

}

public String toString() {

StringBuilder sf = new StringBuilder("Lista:[");

if(!this.isEmpty()) {

Node aux = this.head;

while(null != aux) {

sf.append(aux.toString());
sf.append(",");

aux = aux.getNext();
}
}

sf.append("]");

return sf.toString();
}
}

class MyListUtils {

private MyListUtils(){}

public static double getAverage(MyList myList) {

double sum = 0;
int counter = 0;

if(!myList.isEmpty()) {

Node aux = myList.getHead();

while(null != aux) {

sum += aux.getData();
counter++;

aux = aux.getNext();
}
}

return sum/counter;

}


public static double getStandarDeviation(MyList myList) {

double media = MyListUtils.getAverage(myList);
double sum = 0;
int counter = 0;

if(!myList.isEmpty()) {

Node aux = myList.getHead();

while(null != aux) {

int data = aux.getData();

sum += Math.pow((data-media), 2d);

counter++;

aux = aux.getNext();
}
}

return Math.sqrt(sum/counter);

}

}


public class TestMyList {

public static void main(String[] args) {

[indent]MyList myList = new MyList();
myList.add(new Node(1));
myList.add(new Node(2));
myList.add(new Node(3));
myList.add(new Node(4));
myList.add(new Node(5));

double media = MyListUtils.getAverage(myList);
double standarDeviation = MyListUtils.getStandarDeviation(myList);

System.out.println(myList.toString());
System.out.println("Media: "+media);
System.out.println("Standar deviation: "+standarDeviation);

}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar