{Klausur 18}

Aufgabe 1

a)

b)

5, 3, 2,
4, 
1, 2, 

Aufgabe 2

a)

Node end = new Node(7, null);
end.next = end;

b)

end.next = new Node(8, end.next);
end = end.next;

c)

end.next = end.next.next;

d)

Node p = end.next;
do {
	System.out.println(p.data);
	p = p.next;
} while (p != end.next)

Aufgabe 3

51521721374610
1105
415
317
510
1423217715610
122101715
24617
24
12341517
107617
6107
710
610

Aufgabe 4

a)

public boolean contains(int x) {
	Node p = begin;
	while (p != null) {
		if (p.data == x)
			return true;
		p = p.next;
	}
	return false;
}

b)

public void add(int x) {
	if (end == null) {
		begin = end = new Node(null, x);
		return;
	}
 
	end.next = new Node(null, x);
	end = end.next;
}

c)

public void add(List l) {
	Node p = l.begin;
	while (p != null) {
		add(p.data);
		p = p.next;
	}
}

d)

public boolean startsWith(List l) {
	Node p = begin;
	Node q = l.begin;
	
	while (p != null && q != null) {
		if (p.data != q.data) {
			return false;
		}
		p = p.next;
		q = q.next;
	}
	/*if (q != null)
		return false;
		
	return true;*/
	return q == null;
}

Aufgabe 5

a)

b)

public void prettyPrint() {
	System.out.println(root.data);
	recursivePrint(1, root);
}
 
public void recursivePrint(int n, Node r) {
	for (Node p : r.children) {
		for (int i = 0; i < n; i++) {
			System.out.print("\t");
		} 
		System.out.println(p.data);
		recursivePrint(n + 1, p);
	}
}