Mostrando entradas con la etiqueta Listas. Mostrar todas las entradas
Mostrando entradas con la etiqueta Listas. Mostrar todas las entradas

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:

sábado, 24 de agosto de 2013

Lista

Interfaz a Utilizar:
package Lista;

/**
 * @author daniel199410
 */

public interface IList<E>{
    public void add(E target);
    public boolean isEmpty();
    public boolean isfull();
    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();
}
La clase Lista:
package Lista;

/**
 * @author daniel199410
 */
public class Lista<E>{
    
    E[] data;
    int size;
    
    public Lista(){
        data=((E[])new Object[1]);
        size=0;
    }
    
    /**
     * Agrega un elemento al final de la lista
     */
    public void add(E target) {
        if(isfull())
            stretch();
        data[size]=target;
        size++;
    }
     
    /**
     * Agrega un elemento en un lugar específico de la lista
     */ 
    public void add(E target, int index){
        if(isfull())
            stretch();
        int j=0;
        E[] temp=((E[]) new Object[data.length]);
        System.arraycopy(data, index, temp, 0, 
                         data.length-index-1);
        for(int i=index+1; i<size; i++){
            data[i]=temp[j];
            j++;
        }
        data[index]=target;
        size++;
    }
     
    public boolean contains(E target) {
        for(int i=0; i<size; i++){
            if(data[i].equals(target))
                return true;
        }
        return false;
    }
     
    public E get(int index) {
        if(index>0 && index<size)
            return data[index];
        return null;
    }
     
    public boolean isEmpty() {
        return size==0;
    }
     
    public boolean isfull() {
        return data.length==size;
    }
    
    /**
     * Imprime toda la lista
     */
    public void print() {
        System.out.print("[");
        for(int i=0; i<size-1; i++){
            if(i!=size-2)
                System.out.print(data[i]+",");
            else
            System.out.print(data[i]);
        }
        System.out.println("]");
    }
    
    /**
     * Remueve el elemento que se encuentre en la posición index
     */
    public E remove(int index) {
        if(index>=0 && index<size){
            E element=data[index];
            for(int i=index; i<size; i++)
                data[i]=data[i+1];
            size--;          
            return element;
        }       
        return null;
    }
    
    /**
     * Remueve el primer elemento que sea igual al target
     */
    public boolean remove(E target) {
        for(int i=size-1; i>=0; i--){        
            if(target.equals(data[i])){
                remove(i);
            }
        }
        return false;
    }
    
    /**
     * Remueve todos los elementos que sean iguales a target
     */
    public boolean removeAll(E target){
        boolean x=true;
        while(x==true){
            x=remove(target);
        }
        return x;
    }
     
    public void set(int index, E target) {
        if(index>=0 && index<size)
            data[index]=target;
    } 
    
    /**
     * Aumenta la capacidad de la lista el doble
     */
    private void stretch(){
        E[] temp=(E[])new Object[data.length*2];
        /**
         * arrayCopy(Object src, int srcPos, Object dest,  
         *          int destPos, int length);
         * src     - El array de origen
         * srcPos  - Posción de inicio en el array de origen
         * dest    - El array de destino
         * destPos - Posición de inicio en el array de destino
         * length  - EL número de elementos del array que va a 
         *           ser copiados
         */
        System.arraycopy(data, 0, temp, 0, data.length);
        data=temp;
    }     
} 
Clase Principal
package Lista;

/**
 * @author daniel199410
 */

public class Main {
    
    public static void main(String[] args) {
        
        Lista l1=new Lista();
        l1.add("100");
        l1.add("200");
        l1.add("300");
        l1.add("400");
        l1.add("500");
        l1.add("600");
        l1.add("700");
        l1.add("800");
        l1.add("900");
        l1.add("100");
        l1.add("200", 4);
        l1.add("300", 4);
        l1.add("400", 4);
        l1.add("500", 4);
        l1.add("600", 4);
        l1.add("700", 4);
        l1.add("800", 4);
        l1.add("900", 4);
        l1.add("100", 4);
        l1.add("200", 4);
        l1.add("300", 4);
        l1.add("400", 4);
        l1.add("500", 4);
        l1.add("600", 4);
        l1.add("700", 4);
        l1.add("800", 4);
        l1.add("900", 4);
        l1.add("100", 4);
        l1.add("200", 4);
        l1.add("300", 4);
        l1.print();
        l1.removeAll("900");
        l1.print();
    }
}