lunes, 26 de agosto de 2013

Listas de Nodos

Interfaz a Utilizar:

package Lista_de_Nodos;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public interface INodeList<E>{
    public void add(E target);
    public boolean isEmpty();
    public boolean contains(E target);
    public void set(int index, E target);
    public E get(int index);
    public E remove(int index);
    public boolean remove(E target);
    public boolean removeAll(E target);
    public void print();
    public void swap(int index1, int index2);
}

La clase NodeList

package Lista_de_Nodos;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class NodeList<E> implements INodeList<E>{
    
    private E item;
    private NodeList<E> next, raiz, top;
    private int size;
    
    public NodeList(E item){
        this.item=item;
    }
    
    public NodeList(NodeList next){
        this.next=next;
    }
    
    public NodeList(E item, NodeList next){
        this.item=item;
        this.next=next;
    }
    
    public E getItem() {
        return item;
    }

    public void setItem(E item) {
        this.item = item;
    }

    public NodeList<E> getNext() {
        return next;
    }

    public void setNext(NodeList<E> next) {
        this.next = next;
    }

    public NodeList<E> getRaiz() {
        return raiz;
    }

    public void setRaiz(NodeList<E> raiz) {
        this.raiz = raiz;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
    
    public void add(E target) {
        NodeList nuevo=new NodeList(target);
        if(isEmpty()){
            raiz=nuevo;
            top=nuevo;
            size++;
        }else{
            top.next=nuevo;
            top=nuevo;
            size++;
        }
    }

    public boolean isEmpty() {
        return raiz==null;
    }

    public boolean contains(E target) {
        if(!isEmpty()){
            for(NodeList node=raiz; node!=null; node=node.next){
                if(node.item==target)
                    return true;
            }
        }
        return false;
    }

    public void set(int index, E target) {
        NodeList nuevo=new NodeList(target);
        System.out.println("size "+size);
        if(index>=0 && index<=size){
            if(index==0){
                if(isEmpty()){
                    nuevo.next=null;
                    raiz=nuevo;
                }else{
                    nuevo.next=raiz;
                    raiz=nuevo;
                }
                size++;
            }else{
                if(index==size){
                    if(isEmpty()){
                        raiz=nuevo;
                        top=nuevo;
                    }else{
                        top.next=nuevo;
                        top=nuevo;
                    }
                    size++;
                }else{
                    NodeList temp=raiz;
                    for(int i=0; i<=index-2; i++){
                        temp=temp.next;
                    }
                    NodeList siguiente=temp.next;
                    temp.next=nuevo;
                    nuevo.next=siguiente;
                    size++;
                }
            }               
        }
    }

    public E get(int index) {
        NodeList<E> temp=raiz;
        for(int i=0; i<index; i++){
            temp=temp.next;
        }
        return temp.item;
    }

    public E remove(int index) {
        if(index<=size && index>=0){
            E info;
            if(index==0){
                info=raiz.item;
                raiz=raiz.next;
                size--;
            }else{
                NodeList temp=raiz;
                for(int i=0;i<index-1; i++)
                    temp=temp.next;
                NodeList<E> siguiente=temp.next;
                temp.next=siguiente.next;
                info=siguiente.item;
                size--;
            }
            return info;
        }
        return null;
    }

    public boolean remove(E target) {
        NodeList node=raiz;
        if(contains(target)){
            for(int i=0; i<=size; i++){
                if(node.item.equals(target)){
                    remove(i);
                    return true;
                }else{
                    node=node.next;
                }
            }           
        }
        return false;
    }

    public boolean removeAll(E target) {
        boolean x=true;
        while(x==true){
            x=remove(target);
        }
        return x;
    }
    
    /**
     * Cambia de posición dos elementos
     */
    public void swap(int index1, int index2){
        if(index1>=0 && index1<=size && index2>=0 && index2<=size){
            NodeList<E> temp1=raiz;
            NodeList<E> temp2=raiz;
            for(int i=0; i<index1; i++)
                temp1=temp1.next;
            for(int i=0; i<index2; i++)
                temp2=temp2.next;
            E aux=temp1.item;
            temp1.item=temp2.item;
            temp2.item=aux;
        }else{
            System.out.println("El valor ingresado es mayor que "
                    + "el número de elementos en la lista");
        }
    }

    public void print() {
        System.out.print("[");
        for(NodeList node=raiz; node!=null; node=node.next){
            if(node.next==null)
                System.out.print(node.item);
            else
                System.out.print(node.item+",");
        }
        System.out.println("]");
    }   
}
La clase principal

package Lista_de_Nodos;

/**
 * @author Daniel Cataño Restrepo 
 */
public class Main {

    public static void main(String[] args) {
        NodeList list=new NodeList(1);
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.print();
        System.out.println(list.contains(1));
        System.out.println(list.contains(100));
        list.set(0, "7");
        list.print();
        list.set(7, "8");
        list.print();
        list.set(2, "9");
        list.print();
        list.set(4, "10");
        list.print();
        System.out.println(list.get(0));
        System.out.println(list.get(5));
        list.remove("1");
        list.print();
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.print();
        list.removeAll("1");
        list.print();
        list.remove("5");
        list.print();
        list.remove(5);
        list.print();
        list.swap(4, 6);
        System.out.print("Después del swap: ");
        list.print();
        list.swap(3, 8);
        list.print();
    }
}
Lo Que imprime en pantalla:

No hay comentarios:

Publicar un comentario