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:

domingo, 25 de agosto de 2013

Pila de Nodos

Interfaz a Utilizar:

package Pilas_de_Nodos;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public interface INodeStack<E>{
    public boolean isEmpty();
    public E peek();
    public E pop();
    public void print();
    public void push(E target);
}
La clase NodeStack

package Pilas_de_Nodos;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class NodeStack<E> implements INodeStack<E>{
    
    private NodeStack<E> next;
    private E item;
    private NodeStack<E> raiz;
    
    public NodeStack(E item){
        this.item=item;
    }
    
    public NodeStack(NodeStack<E> next){
        this.next=next;
    }
    
    public NodeStack(E item, NodeStack<E> next){
        this.item=item;
        this.next=next;
    }
    
    public NodeStack<E> getNext() {
        return next;
    }

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

    public E getItem() {
        return item;
    }

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

    public NodeStack getRaiz() {
        return raiz;
    }

    public void setRaiz(NodeStack raiz) {
        this.raiz = raiz;
    }
    
    public boolean isEmpty() {
        return raiz==null;
    }

    public E peek() {
        E firstElement=raiz.item;
        if(!isEmpty()){
            if(raiz.next==null)
                raiz=null;
            else
                raiz=raiz.next;
            return firstElement;
        }
        return null;
    }

    public E pop() {
        return raiz.item;
    }

    public void print() {
        System.out.print("[");       
        for(NodeStack temp=raiz; temp!=null; temp=temp.next){
            if(temp.next==null)
                System.out.print(temp.item);
            else
                System.out.print(temp.item+",");
        }
        System.out.println("]");
    }

    public void push(E target) {
        NodeStack<E> nuevo=new NodeStack<>(target);
        if(isEmpty()){
            nuevo.next=null;
            raiz=nuevo;
        }else{
            nuevo.next=raiz;
            raiz=nuevo;
        }       
    }   
}
La clase principal

package Pilas_de_Nodos;
/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class Main {

    public static void main(String[] args) {
        NodeStack pila=new NodeStack(1);
        pila.push(1);
        pila.print();
        pila.push(2);
        pila.print();
        pila.push(3);
        pila.print();
        pila.push(4);
        pila.print();
        pila.push(5);
        pila.print();
        pila.push(6);
        pila.print();
        pila.peek();
        pila.print();
        pila.peek();
        pila.print();
        pila.peek();
        pila.print();
        pila.peek();
        pila.print();
        pila.peek();
        pila.print();
        System.out.println(pila.pop());
    }
}
Lo Que imprime en pantalla:

Cola de Nodos

Interfaz a Utilizar:

package Colas_de_Nodos;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public interface INodeQueue<E>{
    public void add(E target);
    public boolean isEmpty();
    public void print();
    public E remove();
}
La clase NodeQueue

package Colas_de_Nodos;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */

public class NodeQueue<E> implements INodeQueue<E>{
    
    private E item;
    private NodeQueue<E> next;
    private NodeQueue<E> raiz, top;
    
    public NodeQueue(E item){
        this.item=item;
    }
    
    public NodeQueue(E item, NodeQueue<E> next){
        this.item=item;
        this.next=next;
    }
    
    public NodeQueue(NodeQueue<E> next){
        this.next=next;
    }
    
    public E getItem() {
        return item;
    }

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

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

    public void setNext(NodeQueue<E> next) {
        this.next = next;
    }
    
    public void add(E target) {
        NodeQueue<E> nodoNuevo=new NodeQueue<>(target, null);
        if(isEmpty()){
            raiz=nodoNuevo;
            top=nodoNuevo;
        }else{
            top.next=nodoNuevo;
            top=nodoNuevo;
        }
    }

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

    public void print() {
        System.out.print("[");
        for(NodeQueue temp=raiz; temp!=null; temp=temp.next){
            if(temp==top)
                System.out.print(temp.item);
            else
                System.out.print(temp.item+",");            
        }
        System.out.println("]");
    }

    public E remove() {
        if(!isEmpty()){
            E info=raiz.item;
            if(raiz==top){
                raiz=null;
                top=null;
            }else{
                raiz=raiz.next;
            }
            return info;
        }
        return null;
    }    
}
La clase principal

package Colas_de_Nodos;
/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class Main {

    public static void main(String[] args) {
        NodeQueue cola=new NodeQueue(1);
        cola.add(3);
        cola.add(3);
        cola.add(3);
        cola.add(3);
        cola.add(3);
        cola.add(3);
        cola.print();
        cola.remove();
        cola.print();
        cola.remove();
        cola.print();
        cola.remove();
        cola.print();
        cola.remove();
        cola.print();
        cola.remove();
        cola.print();
        cola.remove();
        cola.print();
        cola.add(3);
        cola.print();
        cola.add(3);
        cola.print();
        cola.add(3);
        cola.print();
        cola.add(3);
        cola.print();
        cola.add(3);
        cola.print();
        cola.add(3);
        cola.print();
    }
}
Lo Que imprime en pantalla:

Colas

Interfaz a Utilizar:

package Colas;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public interface IQueue<E>{
    public void add(E target);
    public boolean isEmpty();
    public void print();
    public E remove();
}
La clase Queue

package Colas;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class Queue<E> implements IQueue<E>{
    
    E[] data;
    
    public Queue(){
        data=(E[])new Object[0];
    }
    
    /**
     * Agrega elemento al final de la cola
     */
    public void add(E target) {
        E[] temp=(E[])new Object[data.length+1];
        if(!isEmpty())
            for(int i=0; i<data.length; i++)
                temp[i]=data[i];
        temp[data.length]=target;
        data=temp;
    }

    public boolean isEmpty() {
        return data.length==0;
    }

    public void print() {
        System.out.print("[");
        for(int i=0; i<data.length; i++){
            if(i==data.length-1)
                System.out.print(data[i]);
            else
                System.out.print(data[i]+",");
        }
        System.out.println("]");
    }
    
    /**
     * Remueve el primer elemento de la estructura y lo retorna
     */
    public E remove() {
        if(!isEmpty()){
            E firstElement=data[0];
            E[]temp=(E[])new Object[data.length-1];
            for(int i=0; i<data.length-1; i++)
                temp[i]=data[i+1];
            data=temp;
            return firstElement;
        }
        return null;
    }   
}
La clase principal

package Colas;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class Main {

    public static void main(String[] args) {
        Queue q=new Queue();
        q.add(1);
        q.add(2);
        q.add(3);
        q.add(4);
        q.add(1);
        q.add(2);
        q.add(3);
        q.add(4);
        q.print();
        q.remove();
        q.remove();
        q.print();
    }
}

Pilas

Interfaz a utilizar
package Pila;

/**
 * @author Daniel Cataño Restrepo
 */
public interface IPila<E&gt{
    public boolean isEmpty();
    public E peek();
    public E pop();
    public void print();
    public void push(E target);
}
La clase Pila
package Pila;
/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class Pila<E> implements IPila<E>{
    E[] data;
    
    public Pila(){
        data=(E[])new Object[0];
    }
    
    public boolean isEmpty() {
        return data.length==0;
    }

    public E peek() {
        return data[0];
    }
    
    /**
     * Remueve el primer elemento de la pila, y lo retorna
     */
    public E pop() {
        E firstElement=data[0];
        E[] temp=(E[])new Object[data.length-1];
        for(int i=0; i<data.length-1; i++)
            temp[i]=data[i+1];
        data=temp;
        return firstElement;
    }

    public void print() {
        System.out.print("[");
        for(int i=0; i<data.length; i++){
            if(i==data.length-1)
                System.out.print(data[i]);
            else
                System.out.print(data[i]+", ");
        }
        System.out.println("]");
    }
    
    /**
     * Agrega un nuevo elemento a la pila
     */
    public void push(E target) {
        E[] temp=(E[])new Object[data.length+1];
        for(int i=data.length-1; i>=0; i--){
            temp[i+1]=data[i];
        }
        temp[0]=target;
        data=temp;
    }
}
La clase Principal

package Pila;

/**
 * @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
 */
public class Main {
    
    public static void main(String[] args) {
       Pila p=new Pila();
       p.push(1);
       p.push(2);
       p.push(3);
       p.push(4);
       p.push(1);
       p.push(2);
       p.push(3);
       p.push(4);
       p.print();
       p.pop();
       p.pop();
       p.pop();
       p.pop();
       p.print();
    }
}

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();
    }
}

viernes, 1 de marzo de 2013

public class Main {
 
    public static void main(String[] a3d) {
        System.out.println("Holii");
    }
}