Sie befinden sich hier: Termine » Prüfungsfragen und Altklausuren » Prüfungen im Bachelor-Studium (1. - 5. Semester) » aud » Forendiskussionen » testcodezulisten   (Übersicht)

public class Jul16_Listen {
 
	public static void main(String[] args) {	
		Cursor<String> test=new Cursor<String>();
 
		addTest(test,"E","D","C","B", "A");
		test.print();
 
		String t=prviousTest(test, null);
		System.out.println("prev = "+t);
		test.print();
 
		String n=nextTest(test,"A");
		System.out.println("next = "+n);
		test.print();
 
		test.remove();
		test.print();
	}
 
	private static String nextTest(Cursor<String> test, String expect) {
		String res=test.next();
		if(res != null){
			org.junit.Assert.assertTrue(res.equals(expect));
		}else if(expect == null){
			org.junit.Assert.assertNull(res);
		}else{
			res="NULL";
		}
		return res;
	}
 
	private static String prviousTest(Cursor<String> test, String expect) {
		String res=test.previous();
		if(res != null){
			org.junit.Assert.assertTrue(res.equals(expect));
		}else if(expect == null){
			org.junit.Assert.assertNull(res);
		}else{
			res="NULL";
		}
		return res;
	}
 
	public static void addTest(Cursor<String> test, String... vals){
		for(String v:vals){
			test.add(v);
		}
	}
 
 
	private static ListItem createSimple() {
		ListItem a=new ListItem("A");
		ListItem b=new ListItem("B");
		ListItem c=new ListItem("C");
		ListItem d=new ListItem("D");
		ListItem e=new ListItem("E");
		a.addItem(a, b);
		b.addItem(a, c);
		c.addItem(b, d);
		d.addItem(c, e);
		e.addItem(d, a);
		return a;
	}
}
 
class Cursor<E> implements ListIterator<E> {
 
	private class Node {
		private E v; // value (payload)
		private Node p, n; // previous, next
		public String toString(){
			return v+"";
		}
	}
	private Node prev, next;
 
	public void add(E e) {
		Node newN = new Node(); // new node becomes next
		newN.v = e;
 
		if(next != null){
			next.p=newN;
			newN.n=next;
		}
 
		if(prev != null){
			prev.n=newN;
			newN.p=prev;
		}
 
		next=newN;
	}
 
	@Override
	public E previous() {
		// TODO Auto-generated method stub
		if(prev != null){
			E vTmp=prev.v;
			next=prev;
			prev=prev.p;
			return vTmp;
		}
		return null;
	}
 
	@Override
	public void remove() {
		if(next != null){
			next=next.n;
		}
 
	}
	public String toString(){
		String res="";
 
		Node cur=this.next;
		Node drag=this.prev;
 
		res=res+drag + "-->";
		while(cur != drag && cur != null){	
			drag=cur;
			cur=cur.n;
			if(cur == drag){
				res=res+drag + "-->"+cur;
			}else if(drag.n == null){
				res=res+drag + "-->"+"null";
			}
			else{
				res=res+drag + "-->";
			}
		}	
		return res;
	}
	public void print() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		System.out.println("       /                 \\ ");
		System.out.println("      |   MONSTER CURSOR  |");
		System.out.println("       \\                 /");
		System.out.println("            /        \\");
		System.out.println("           /          \\");
		  System.out.print("      ");
		  System.out.print(this.prev != null?"    "+this.prev.v:" null");
		                System.out.print("        ");
		                System.out.println(this.next != null?"    "+this.next.v:"null");
		System.out.println();
		System.out.println(this.toString());
		System.out.println("--------------------------------------");
		System.out.println();
	}
 
	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return false;
	}
 
	@Override
	public E next() {
		// TODO Auto-generated method stub
		if(next != null){
			E vTmp=next.v;
			prev=next;
			next=next.n;
			return vTmp;
		}
		return null;
	}
 
	@Override
	public boolean hasPrevious() {
		// TODO Auto-generated method stub
		return false;
	}
 
 
 
	@Override
	public int nextIndex() {
		// TODO Auto-generated method stub
		return 0;
	}
 
	@Override
	public int previousIndex() {
		// TODO Auto-generated method stub
		return 0;
	}
 
 
 
	@Override
	public void set(E e) {
		// TODO Auto-generated method stub
 
	}
}