Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 1 Algorithmen und Datenstrukturen 1 SS 2002 Mag.Thomas.

Ähnliche Präsentationen


Präsentation zum Thema: "Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 1 Algorithmen und Datenstrukturen 1 SS 2002 Mag.Thomas."—  Präsentation transkript:

1 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 1 Algorithmen und Datenstrukturen 1 SS 2002 Mag.Thomas Hilpold Institut für Wirtschaftsinformatik Software Engineering JKU Linz Termin 6 – rek. Datentypen, verkettete Listen

2 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 2 Lösung Übung 4 Themenbereiche heute rekursive Datentypen einfach verkettete Liste Sortiertes Einfügen Übungsbesprechung Übung 6 Übersicht

3 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 3 Lösung Ü4 boolean intersects(↓float r1x, ↓float r1y, ↓float r1width, ↓float r1height, ↓float r2x, ↓float r2y, ↓float r2width, ↓float r2height) { return (r2x + r2width >= r1x && // rechte kante r2 rechts neben linke Kante r1 r2y + r2height >= r1y && r2x <= r1x + r1width && //und: linke Kante r2 links neben rechter Kante r1 r2y <= r1y + r1height); // } Verbunde, Verbunde mit Operationen r1r2a r2c r2b r2d

4 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 4 Lösung Ü4 (2) // prüft, ob das Rechteck r2 vollständig im Rechteck r1 enthalten ist. boolean contains (↓float r1x, ↓float r1y, ↓float r1width, ↓float r1height, ↓float r2x, ↓float r2y, ↓float r2width, ↓float r2height) { return (r2x >= r1x && r2y >= r1y && (r2x + r2width) <= r1x + r1width && (r2y + r2heigth) <= r1y + r1height); }

5 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 5 Lösung Ü4 (3) // erzeugt ein neues Rechteck r3, das die Schnittmenge der Rechteck r1 und r2 enthält. // falls keine Schnittmenge –1,-1,-1,-1 createIntersection(↓float r1x, ↓float r1y, ↓float r1width, ↓float r1height, ↓float r2x, ↓float r2y, ↓float r2width, ↓float r2height, ↑float r3x, ↑float r3y, ↑float r3width, ↑float r3height) { if (intersects(r1x, r1y, r1widht, r1height, r2x, r2y, r2width, r2 height) { r3x = Math.max(r1x, r2x); r3y = Math.max(r1y, r2y); r3width = Math.min(r1x +r1width, r2x + r2width) - r3x; r3height = Math.min(r1y +r1heigth, r2x + r2height) – r3y; }else { r3x = -1; r3y = -1; r3width = -1; r3heigth = -1; }

6 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 6 Lösung Ü4 (4) type Rectangle = { float x; float y; float width; float height; } Rectangle createIntersection(↓Rectangle r1, ↓Rectangle r2) // Funktion statt Methode oder createIntersection(↓Rectangle r1, ↓Rectangle r2, ↑Rectangle r3) { // Methode if (intersects(r1, r2) { r3.x = Math.max(r1.x, r2.x); r3.y = Math.max(r1.y, r2.y); r3.width = Math.min(r1.x +r1.width, r2.x + r2.width) - r3.x; r3.height = Math.min(r1.y +r1.heigth, r2.x + r2.height) – r3.y; }else { r3.x = -1; r3.y = -1; r3.width = -1; r3.heigth = -1; }

7 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 7 Lösung Ü4 (5) type Rectangle = { float x; float y; float width; float height;... Rectangle createIntersection(↓Rectangle r) boolean intersects(↓Rectangle r)... } Rectangle createIntersection(↓Rectangle r) { Rectangle ret; if (intersects(r) { ret.x = Math.max(x, r.x); ret.y = Math.max(y, r.y); ret.width = Math.min(x +width, r.x + r.width) – ret.x; ret.height = Math.min(y +heigth, r.x + r.height) – ret.y; }else { ret.x = -1; ret.y = -1; ret.width = -1; ret.heigth = -1; } return ret; }

8 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 8 Lösung Ü4 (6) Algorithmen mit Gedächtnis // totalDistance... Distanz seit erstem Aufruf in KM ; actSpeed... Momentangeschwindigkeit in KM/H pulse(↑float totalDistance, ↑float actSpeed) { final float DIAMETER_OF_TYRE = 2200/3.14159265; // Reifendurchmesser mm final float CIRCUMFERENCE = DIAMETER * PI / 1E3 //– in Meter static int pulseCount = 0; // Anzahl der Aufrufe static float lastCallTime // first initialization static boolean validLastCallTime = FALSE if (!validLastCallTime){ validLastCallTime = TRUE lastCallTime = getTime() totalDistance = 0.0 actSpeed = 0.0 return // returns !! } float curCallTime = getTime(); float delay = curCallTime – lastCallTime; pulseCount++; totalDistance = pulseCount * CIRCUMFERENCE / 1000; // in KM // assert delay > 0 actSpeed = (CIRCUMFERENCE) / delay actSpeed = actSpeed / 3.6; // m/sec -> km/h if (actSpeed < 1) actSpeed = 0; // erst ab 1 km/h sinnvoller Wert lastCallTime = curCallTime;// merke letzte Zeit }

9 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 9 Rückblick Datentypen elementare Datentypen Verbunde (type) werden implizit erzeugt keine Referenzen, Variable repräsentiert ganzes Datenobjekt Referenzdatentypen (refType) dynamisch verwaltete Datenobjekte Referenzvariable werden explizit erzeugt und gelöscht (new/delete)

10 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 10 Rekursive Datentypen Allgemeines Ein rekursiver Datentyp enthält Referenzvariablen vom eigenen Typ. Referenzdatentypen sind eine Voraussetzung für rekursive Datentypen. Beispiel: refType Node = { int value Node next } Node first, other first = new Node; first^.value = 1; other = new Node; other^.value = 2; first^.next = other; other = new Node; other^.value = 3; first^.next^.next = other;

11 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 11 Rekursive Datentypen Allgemeines Ein rekursiver Datentyp enthält Referenzvariablen vom eigenen Typ. (Voraussetzung: Referenztypen !!) Mit rekursiven Datentypen kann man dynamische Datenstrukturen realisieren (Listen, Bäume,...) Beispiel: refType Node = { int value Node next } Node first, other first = new Node; first^.value = 1; other = new Node; other^.value = 2; first^.next = other; other = new Node; other^.value = 3; first^.next^.next = other; value = 1 next value = 2 next value = 3 next first

12 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 12 Verkettete Listen Allgemeines Verkettete Listen sind dynamische Datenstrukturen. Sie bestehen aus Knoten (nodes, rekursiver Datentyp), die –je nach Listenart- auf Nachfolgeknoten und/oder Vorgängerknoten zeigen. value = 1 next value = 2 next value = 3 next head Beispiel – einfach verkettete Liste Operationen ?

13 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 13 Einfach verkettete Liste Operationen vorne einfügen (insertFirst) hinten anfügen (append) löschen (delete) (eines, ganze Liste) value = 1 next value = 2 next value = 3 next head Beispiel Ziel: einfach verkettete Liste mit Knoten, die int-Wert enthalten. Realisierung: Referenzdatentyp mit oder ohne Operationen?

14 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 14 Einfach verkettete Liste ohne und mit List value = 1 next value = 2 next value = 3 next first reftype Node = { int value Node next } append(  Node node,  int a) f() { Node first; append(  first,  12) } type List = { reftype Node = { int value Node next } // Node Node first // ! init() { first = null } append(  int a) // zu List ! } // List f() { List l; l.init() append(  10) }

15 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 15 Einfach verkettete Liste insertFirst value = 5 next first List l; l.init() l.insertFirst(  5) // 1 Liste leer, first zeigt auf null l.insertFirst(  3) // 2 Einfügen an Beginn der Liste. Liste kann leer oder ein oder mehrere Elemente enthalten. first zeigt immer auf das erste Element oder null (== leer) nach 1 value = 3 next first nach2 value = 5 next

16 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 16 Einfach verkettete Liste insertFirst value = 5 next first insertFirst(int a) { Node newElem newElem = new Node newElem^.value = a newElem^.next = first // neues Element zeigt auf bisherigs erstes first = newElem// und wird erstes Element der Liste } nach 1 value = 3 next first nach2 value = 5 next

17 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 17 Einfach verkettete Liste insertLast (hinten anfügen), append value = 5 next first List l; l.init() l.insertLast(  5) // 1 l.insertLast(  3) l.insertLast(  4) // 2 nach 1 value = 5 next first nach2 value = 3 next value = 4 next hier Sonderfall, Liste leer 1. suche Letztes (cur^.next == null) 2. Letztes soll auf Angehängtes zeigen 3. Angehängtes zeigt auf null

18 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 18 Einfach verkettete Liste insertLast(  int a) { Node newElem newElem = new Node newElem^.value = a newElem^.next = null if (first != null) { // sonderfall leer first = newElem } else { // suche,... Node cur cur = first while (cur^.next != null) { cur = cur^.next } cur^.next = newElem } value = 5 next first nach2 value = 3 next value = 4 next 1. suche Letztes (cur^.next == null) 2. Letztes soll auf Angehängtes zeigen 3. Angehängtes zeigt auf null

19 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 19 Einfach verkettete Liste delete (löschen der Liste) l.delete() // alle Elemente werden gelöscht // first zeigt auf null, liste leer value = 5 next first value = 3 next value = 4 next 1. durchlaufe liste 2. falls aktuelles nicht null -> merke nächstes lösche aktuelles

20 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 20 Einfach verkettete Liste delete() { Node cur, next; cur = first while (cur != null) { next = cur^.next delete cur cur = next; } first = null; } value = 5 next first value = 3 next value = 4 next starte mit first durchlaufe liste solange aktuelles nicht null{ merke Nachfolger lösche aktuelles aktuelles = Nachfolger }

21 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 21 Einfach verkettete Liste Zusammenfassung value = 5 next first value = 3 next value = 4 next unsortierte Liste Verbund Liste mit Referenzdatentyp node Liste hat einige Operationen (insertFirst, insertLast, delete)

22 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 22 Einfach verkettete Liste Sortierung value = 1 next first value = 2 next value = 3 next Ziel: aufsteigend sortierte Liste nötig: Operation insert(  int a ) insertFirst, insertLast kann Sortierung zerstören. Löschen eines Elements ändert nichts an der Sortierung. Lösungsidee: 1.Neues Erzeugen (trivial) 2.Position suchen 3.Einfügen (Sonderfall, erstes, letztes)

23 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 23 Sortieres Einfügen in Liste Suche der Einfügeposition value = 1 next first value = 5 next value = 7 next cur = first; prev = null while (cur != null && cur^.value < newElem.value) { prev = cur // prev nötig, da prev^.next nötig cur = cur^.next } // prev zeig // einfügen als erstes ? prev = null, cur egal, Liste leer // einfügen in der mitte ? prev != null // einfügen als letztes ? prev != null, cur == null value = 3 next prev cur

24 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 24 Sortieres Einfügen in Liste Einfügen (1. Element) value = 1 next first value = 5 next value = 7 next if (prev == null) { first = newElem; newElem^.next = cur; // auch wenn cur == null, also Liste leer } value = 0 next prevcur

25 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 25 Sortieres Einfügen in Liste Einfügen (Mitte, Ende) value = 1 next first value = 5 next value = 7 next if (prev != null) { prev^.next = newElem; newElem^.next = cur; // auch wenn cur == null, also Ende } value = 6 next prev cur

26 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 26 Sortieres Einfügen in Liste Einfügen insertSorted(  int a) { Node newElem, cur, prev newElem = new Node newElem^.value = a //position suchen cur = first; prev = null while(cur != null && cur^.value < newElem.value) { prev = cur cur = cur^.next } if (prev != null) { // Mitte oder Ende prev^.next = newElem } else { first = newElem } newElem^.next = cur } // end insertSorted

27 Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 27 Übung 6 ad 1) StudentList Doppelt verkettete nach Matrikelnummern sortierte Liste reftype Student nicht verändern (Student statt int in Übung) Liste erzeugt und löscht nur Knotenelemente, keine Students remove(int matNr) // Annahme eindeutig ?? printAscending / printDescending – Liste von first oder last beginnend ad 2) TODO-List insertTask(  Task t) boolean next(  Task t) absteigend sortiert einfügen -> höchste Priorität zuerst Task ist ein Verbundtyp


Herunterladen ppt "Mag. Thomas Hilpold, Universität Linz, Institut für Wirtschaftsinformatik – Software Engineering 1 Algorithmen und Datenstrukturen 1 SS 2002 Mag.Thomas."

Ähnliche Präsentationen


Google-Anzeigen