package constract;

class Node<T> {
    
    private Node<T> node;
    private T value;
    private int length;
    
    public Node(T t){
        value = t;
        node = null;
    }
    
    public Node(T value , Node<T> node){    
        this.value = value;    
        this.node = node;  
    }

    public Node<T> getNode(){
        return this.node;
    }
    
    public T getValue(){
        return this.value;
    }
    
    public void setValue(T t){
        this.value = t;
    }
    
    public void setNode(Node<T> node){
        this.node = node;
    }
    
    public String toString(){
        Node<T> nodes = this;
        StringBuffer str = new StringBuffer().append("[");
        boolean i = true;
        while(i){
            if(nodes.node == null){
                i = false;
            }
            str.append(nodes.value.toString());
            if(i){
                str.append(",");
            }
            nodes = nodes.node;
        }
        return str.append("]").toString();
    }
    
}

 

package constract;

public class Nodes<T> {
    
    private Node<T> node;
    private int at;
    
    public Nodes(){
        this.node = null;
        this.at = 0;
    }
    
    public void add(T t){
        this.node = new Node( t , this.node );
        at++;
    }
    
    public void set(int index , T t){
        if( index < at ){
            Node<T> no = this.node;
            Object[] o = new Object[at+1];
            for(int i = at ; i > index+1 ; i-- ){
                o[i] = no.getValue();
                no = no.getNode();
            }
            no.setValue(t);
            for(int i = index+1 ; i < at ; i++ ){
                no = new Node( (T)o[i+1] , no);
            }
            this.node = no ;
        }else if( index == at ){
            add(t);
        }else{
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+at);
        }
    }
    
    public T get(int index){
        if( index < at ){
            Node<T> no = this.node;
            Object[] o = new Object[at+1];
            for(int i = at ; i > index+1 ; i-- ){
                o[i] = no.getValue();
                no = no.getNode();
            }
            return no.getValue();
        }else{
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+at);
        }
    }
    
    public void remove(int index){
        if( index < at ){
            Node<T> no = this.node;
            Object[] o = new Object[at+1];
            for(int i = at ; i > index ; i-- ){
                o[i] = no.getValue();
                no = no.getNode();
            }
            for(int i = index+1 ; i < at ; i++ ){
                no = new Node( (T)o[i+1] , no);
            }
            at--;
            this.node = no ;
        }else{
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+at);
        }
    }
    
    public int size(){
        return at;
    }
    
    public String toString(){
        if(this.node != null){
            return this.node.toString();
        }else{
            return "[]";
        }
        
    }
    
    public static void main(String[] args){
        Nodes<String> No = new Nodes<String>();
        for(int i = 0 ; i < 10 ; i++ ){
            //No.add("at_"+i);
            No.set(i,"at_"+i);
            System.out.println(No);
        }
        System.out.println(No.size());
        for(int i = 0 ; i < No.size() ; i++ ){
            No.set(i, "this is new "+i);
        }
        for(int i = 0 ; i < No.size() ; i++ ){
            System.out.println(No.get(i));
        }
        while(No.size()>0){
            No.remove(No.size()-1);
            System.out.println(No);
        }
    }
    
}