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
5 | 15 | 2 | 17 | 2 | 1 | 3 | 7 | 4 | 6 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
1 | 10 | 5 | ||||||||
4 | 15 | |||||||||
3 | 17 | |||||||||
5 | 10 | |||||||||
1 | 4 | 2 | 3 | 2 | 17 | 7 | 15 | 6 | 10 | |
1 | 2 | 2 | 10 | 17 | 15 | |||||
2 | 4 | 6 | 17 | |||||||
2 | 4 | |||||||||
1 | 2 | 3 | 4 | 15 | 17 | |||||
10 | 7 | 6 | 17 | |||||||
6 | 10 | 7 | ||||||||
7 | 10 | |||||||||
6 | 10 |
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);
}
}