public class Stack{
	
	private int z=0; //Zaehler fuer Lenght-Methode
	
	class Eintrag{
		Object inhalt;
		Eintrag next;
		Eintrag(Eintrag next, Object inhalt){
			this.next = next;
			this.inhalt = inhalt;
		}
	}
	
	Eintrag S=null;
	
	public void push(Object x){
		if (S!=null)S = new Eintrag(S, x);
		else S = new Eintrag(null, x);
		z++;
	}
	
	public Object pop(){
		if(S==null) return null;
		Object x = S.inhalt;
		S = S.next;
		z--;
		return x;
	}
	
	public int length(){
		return z;
	}
	
	public int length2(){ //2. Moeglichkeit: zaehlt ohne die Zaehlvariable z.
		Eintrag temp = S;
		int z2=0;
		while(temp!=null){
			temp=temp.next;
			z2++;
		}
		return z2;
	}

	public String toString(){
		if(S==null) return "Stack ist leer.";
		else{
			String text = "Stack (Laenge "+length()+")\n";
			Eintrag temp = S;
			int l = length();
	
			while(temp!=null){
				text = text + temp.inhalt + ", ";
				temp = temp.next;
				l--;
			}
			return text;
		}
	}

}