import java.io.*;

class Nodo {
    private Object info;
    private Nodo next;
    public Nodo () {
	info = null;
	next = null;
    }
    public void setInfo (Object o) throws SCLeccezione {
	if (this==null) 
	    throw new SCLeccezione("errore-setInfo");
        else this.info = o;
    }
    public Object getInfo () throws SCLeccezione {
	if (this==null)
	    throw new SCLeccezione("errore-getInfo");
        else return (this.info);
    }
    public Nodo getNext () throws SCLeccezione {
        if (this==null)
	    throw new SCLeccezione("getNext");
	else return (this.next);
    }
    public void setNext (Nodo n) throws SCLeccezione {
        if (this==null)
	    throw new SCLeccezione("setNext");
	else this.next=n;
    }
}
class SCLeccezione extends Exception {
    SCLeccezione (String s) {
	super(s);
    }
}



public class SCL {
    private Nodo nodoinit;
    public SCL () {
	nodoinit = null;
    }
    public boolean empty () {
	return (nodoinit==null);
    }
    public void addElement (Object o) throws SCLeccezione {
	Nodo aux = new Nodo();
	aux.setInfo(o);
	aux.setNext(this.nodoinit);
	nodoinit=aux;
    }
    public Object getFirstElement () throws SCLeccezione {
	if (this.empty())
	    throw new SCLeccezione("errore-getFirstElement");
	else return nodoinit.getInfo();		
    }
    public void print () throws SCLeccezione {
    	if (!this.empty()) {
	    Nodo aux=this.nodoinit;
	    while (aux != null) {
		System.out.println(aux.getInfo());
          	aux=aux.getNext();
	    }
	}
    }
    public int lenght () throws SCLeccezione {
	if (this.empty()) return 0;
        else {	
	    int l=0;
	    Nodo aux = this.nodoinit;
	    while (aux != null) {
		aux=aux.getNext();
		l++;
	    }
	    return l;
	}
    }
    public Object elementAt (int i) throws SCLeccezione {
	if (this.empty())
	    throw new SCLeccezione("errore-elementAt");
	else {
	    int l=0;
	    Nodo aux = this.nodoinit;
	    while (aux != null && i > l) {
		aux=aux.getNext();
		l++;
	    }
	    if (aux != null)
		return aux.getInfo();
	    else throw new SCLeccezione("errore-elementAt");
	}
    }	
    public int countElements (Object o) throws SCLeccezione {
    	if (this.empty()) 
	    return 0;
	else {
	    Nodo aux = nodoinit;
	    int count = 0;
	    while (aux != null) {
		if (aux.getInfo().equals(o))
		    count++;
          	aux=aux.getNext();
	    }
	    return count;
	}
    }
    public boolean findElement (Object o) throws SCLeccezione {
    	if (this.empty()) 
	    return false;
	else {
	    Nodo aux = nodoinit;
	    while (aux != null) {
		if (aux.getInfo().equals(o))
		    return true;
          	aux=aux.getNext();
	    }
	    return false;
	}
    }
}



