Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. J. O. Blech Dipl.-Inform. M. J. Gawkowski Dipl.-Inform. N. Rauch
TU Kaiserslautern
Fachbereich Informatik AG Softwaretechnik
L¨ osungvorschlag zum ¨ Ubungsblatt 10: Software-Entwicklung I (WS 2006/07)
Aufgabe 1 Doppelt verkettete Listen
Beim gleichzeitigen Benutzen der Prozeduren aus den Aufgabenteilen a), b) und d) kann es passieren, dass DDList.first nicht konsistent gesetzt wird. Beim Zusammenf¨ugen mehrerer Listen stellt sich das Problem, was mit Elementen mit gleichem key passiert. Zum Zusammenf¨ugen sind die Proze- duren aus Aufgabenteil d) besser geeignet, weil sie in vielen F¨allen weniger Zeit ben¨otigen werden um ein Element einzuf¨ugen. Dies gilt insbesondere, wenn die Listen Merge Prozedur einen Pointer auf ein Element in der N¨ahe des n¨achsten einzuf¨ugenden Elements h¨alt (was bei einigermaßen effizienten Implementierungen der Fall sein sollte).
class DDList {
DDElement first;
}
class DDElement { DDElement next;
DDElement pred;
int key;
String content;
}
public class uebung10a1 extends InputOutput { public static void main(String [] args) {
DDList l = new DDList();
insert (l,2,"Du");
insert (l,3,"Welt");
insert (l,1,"Hallo");
printlist(l);
delete (l,2);
insert2(l.first,99,"!");
printlist(l);
}
public static void insert(DDList list,int i,String c) { DDElement t = list.first, t2;
if (t == null) {
list.first = new DDElement();
list.first.key = i;
list.first.content = c;
return;
}
if (i < list.first.key) {
list.first = new DDElement();
list.first.key = i;
list.first.content = c;
list.first.next = t;
t.pred = list.first;
return;
}
while (t.next != null & t.key < i) t = t.next;
if (t.key = i) {t.content = c; return;}
t2 = t.next;
t.next = new DDElement();
t.next.key = i;
t.next.content = c;
t.next.pred = t;
t.next.next = t2;
if (t2 != null) t2.pred = t.next;
return;
}
public static void insert2(DDElement e,int i,String c) { DDElement t = e, t2;
if (t.key < i) {
while (t.next != null & t.key < i) t = t.next;
t2 = t.next;
t.next = new DDElement();
t.next.key = i;
t.next.content = c;
t.next.pred = t;
t.next.next = t2;
if (t2 != null) t2.pred = t.next;
return;
}
while (t.pred != null & t.key >= i) t = t.pred;
t2 = t.pred;
t.pred = new DDElement();
t.pred.key = i;
t.pred.content = c;
t.pred.next = t;
t.pred.pred = t2;
if (t2 != null) t2.next = t.pred;
return;
}
public static void delete (DDList list,int i) { DDElement t = list.first, t2;
if (list.first.key == i) {
list.first = list.first.next;
}
while (t != null & t.key != i) t = t.next;
if (t == null) return;
if (t.next != null) t.next.pred = t.pred;
if (t.pred != null) t.pred.next = t.next;
return;
}
public static void delete2 (DDElement e,int i) { DDElement t = e, t2;
while (t != null & t.pred != null) t = t.pred;
while (t != null & t.key != i) t = t.next;
if (t == null) return;
if (t.next != null) t.next.pred = t.pred;
if (t.pred != null) t.pred.next = t.next;
return;
}
public static void printlist(DDList list) { DDElement t = list.first;
while (t != null ) {
println ("key: "+t.key+" Content: "+t.content);
t = t.next;
}
return;
} }
Aufgabe 2 Verifikation
a) s=Pi−1i0=0i02 i < N
b) s=PNi0=0−1i02 i=N
c) Der R¨uckgabewert ist (PNi0=0−1i02) + 10
public void f(nat N) { i = 1;
s = 0;
// i = 1 && s = 0 while (i < N) {
// s = sum (i’=0 ... i-1, i’*i’) && i < N s = s + i * i;
// s = sum (i’=0 ... i, i’*i’) && i < N i = i + 1;
// s = sum (i’=0 ... i-1, i’*i’) && i <= N }
// s = sum (i’=0...N-1, i’*i’) && i = N s = s + 10;
// s = sum (0,N-1, i’*i’) + 10 && i = N return s;
// s = sum (0,N-1, i’*i’) + 10 }