Verkettete Liste

using System;
//Programm für doppelt verkettete Listen


public class Stack{
	public int wert
	{ get; private set; }
	public Stack next;
	public Stack last;
	
	public override string ToString(){
		return this.wert.ToString();
	}
	
	public Stack(int wert){
		this.wert = wert;
	}
}

public class Manager{
	Stack zeiger;
	
	public void push(Stack item){

		if(zeiger == null){
			zeiger = item;
		}else{
			if(zeiger.next != null && zeiger.last != null){
				zeiger.last.next = item;
				item.last = zeiger.last;
				item.next = zeiger;
				zeiger.last = item;
			}else{
				zeiger.last = item;
				item.next = zeiger;
				zeiger = item;
			}
		}	
	}
	public Stack pop(){
		Stack returnStack = zeiger;
		zeiger = zeiger.next;
		return returnStack;
	}
	
	public void nextStack(){
		//geht einen Schritt nach vorne
		if(zeiger != null && zeiger.next != null){
			zeiger = zeiger.next;
		}
	}
	
	public void lastStack(){
		//geht einen Schritt zurück
		if(zeiger != null && zeiger.last != null){
			zeiger = zeiger.last;
		}
	}
	
	public Stack peck(){
		return this.zeiger;
	}
	
	public Stack getHead(){
		//gibt erstes element
		Stack item = zeiger;
		while(item.last != null){
			item = item.last;
		}
		return item;
	}
	
	public int length(){
		int lenght = 1;
		Stack item = getHead();
		while(item.next != null){
			item = item.next;
			lenght++;
		}
		return lenght;
	}
	
	public Stack getTail(){
		//get letztes element
		Stack item = zeiger;
		while(item.next != null){
			item = item.next;
		}
		return item;
	}
	
	public void setZeiger(Stack item){
		this.zeiger = item;
	}
	
	public void AddBefore(int position, Stack element){
		Stack anker = this.zeiger;
		for(int i = 0; i < position; i++){
			this.nextStack();
		}
		this.push(element);
		this.setZeiger(anker);
	}
	
	public void AddAfter(int position, Stack element){
		Stack anker = this.peck();
		for(int i = 0; i < position; i++){
			this.lastStack();
		}
		this.push(element);
		this.setZeiger(anker);
	}
	
	public bool contains(Stack suchItem){
		Stack item = this.getHead();
		while(item.next != null){ // sind wir am ende ? 
			if(item.wert == suchItem.wert){
				return true;
			}
			item = item.next;
		}
		return false;
	}
	public Manager(){}
		
}

public class Program
{
	public static void Main()
	{
		Stack stack = new Stack(42);
		Stack stack2 = new Stack(40);
		Stack stack3 = new Stack(69);
		Stack stack4 = new Stack(100);
		
		Manager manager = new Manager();
		
		manager.push(stack);
		manager.push(stack2);
		manager.push(stack3);
		
		//lenght
		Console.WriteLine(manager.length());
		Console.WriteLine(manager.contains(stack4));
		manager.AddBefore(1, stack4);
		Console.WriteLine(manager.length());
		Console.WriteLine(manager.contains(stack4));
		manager.nextStack();
		Console.WriteLine(manager.peck());
	}
}