Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package ayudantia;
- /**
- *
- * @author EDWARD
- */
- public class Lista {
- Nodo L;
- public Lista()
- {
- L=null;
- }
- public void InsertarP(int a)
- {
- Nodo p= new Nodo(a);
- if (L==null)
- {
- L=p;
- }
- else
- {
- p.setEnlase(L);
- L=p;
- }
- }
- public void InsertarU(int d)
- {
- Nodo t= new Nodo (d);
- if (L==null)
- {
- L=t;
- }
- else
- {
- Nodo aux=L;
- while (aux!=null)
- {
- if (aux.getEnlase()==null)
- {
- aux.setEnlase(t);
- return;
- }
- aux=aux.getEnlase();
- }
- }
- }
- public String mostrar()
- { String s="";
- Nodo aux=L;
- while(aux!=null)
- {
- s=s+" | "+aux.getDato();
- aux=aux.getEnlase();
- }
- return s;
- }
- private boolean vacio()
- {
- if(L==null)
- return true;
- else
- return false;
- }
- public int Suma () throws Exception
- {
- int x=0;
- if(L==null){
- throw new Exception("Error : La lista esta vacia") ;
- }else{
- Nodo P=L;
- while (P!=null ){
- x=x+P.getDato();
- P=P.getEnlase();
- }
- return x;
- }
- }
- public int mayor() throws Exception
- {
- int may;
- Nodo aux=L;
- if (L!=null)
- {
- may=aux.getDato();
- while(aux!=null)
- {
- if (aux.getDato()>may){
- may=aux.getDato();
- }
- aux=aux.getEnlase();
- }
- return may;
- }
- else{
- throw new Exception("Error");
- }
- }
- public void eliminar(int dato){
- Nodo P=L;
- if (vacio())
- {
- }
- else
- {
- if (L.getEnlase()==null)
- {
- if (L.getDato()==dato)
- L=null;
- }
- else
- {
- if (P.getDato()==dato)
- {
- L=L.getEnlase();
- }
- else
- {
- if(P.getEnlase()!=null) {
- while(P!=null)
- {
- if (dato==P.getEnlase().getDato()){
- P.setEnlase(P.getEnlase().getEnlase());
- return;
- }
- else {
- P =P.getEnlase();
- }
- } }
- }
- }
- }
- }
- public boolean esta(int x) {
- Nodo aux=L;
- while(aux!=null){
- if(x==aux.getDato()){
- return true;
- }else{
- aux=aux.getEnlase();
- }
- }
- return false;
- }
- public void eliminarUlt()
- { Nodo aux=L;
- while (aux!=null){
- if (aux.getEnlase().getEnlase()==null)
- aux.setEnlase(null);
- aux=aux.getEnlase();
- }
- }
- public void eliminarPri()
- {
- if(vacio())
- return;
- L=L.getEnlase();
- }
- public void ordenarAsc () throws Exception{
- if (L==null){
- throw new Exception("error");
- }
- else{
- //Nodo aux=L;
- Lista p=new Lista();
- while(L!=null){
- int x=mayor();
- eliminar(x);
- p.InsertarU(x);
- //L=L.getEnlase();
- }
- // Lista aux=p;
- /*while(p.L!=null){
- InsertarP(p.L.getDato());
- p.L=p.L.getEnlase();
- }*/
- L=p.L;
- //return;
- }
- }
- public void Invertir() throws Exception{
- if (L==null){
- throw new Exception("error");
- }
- else{
- //Nodo aux=L;
- Lista p=new Lista();
- while(L!=null){
- int x=L.getDato();
- eliminar(x);
- p.InsertarU(x);
- //L=L.getEnlase();
- }
- // Lista aux=p;
- while(p.L!=null){
- InsertarP(p.L.getDato());
- p.eliminarPri();
- }
- //L=p.L;
- //return;
- }
- }
- //---------------------------------RECURSIVO----------------------------------------
- public int Sumarrr()
- {
- return SumarRec(L);
- }
- private int SumarRec(Nodo p)
- {
- int suma=0;
- if (p==null)
- {
- return suma;
- }
- else
- {
- suma=suma+SumarRec(p.getEnlase());
- return suma+p.getDato();
- }
- }
- public void Elimnarrr(int dato)
- { EliminarR(L, dato);
- }
- private void EliminarR(Nodo p,int dato)
- {
- Nodo P=L;
- if (vacio())
- {
- return;
- }
- else
- {
- if (L.getEnlase()==null)
- {
- if (L.getDato()==dato)
- L=null;
- return;
- }
- else
- {
- if (P.getDato()==dato)
- {
- L=L.getEnlase();
- return;
- }
- else
- {
- if(P.getEnlase()!=null) {
- EliminarR(p.getEnlase(), dato);
- if (dato==P.getEnlase().getDato()){
- P.setEnlase(P.getEnlase().getEnlase());
- return;
- }
- } }
- }
- }
- }
- public void invertirdosendos(){
- Lista p=new Lista();
- while (L!=null){
- int x=0;
- while(x!=2){
- Invertir();
- x++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement