420 likes | 552 Views
Datenstrukturen. Programmierung II Prof. Dr. Michael Löwe. Inhalt. Der Begriff „Datenstruktur“ Datenstrukturen = Datentyp + Prozeduren Funktional Mit Seiteneffekte Datenstrukturen = Programm-Modul Datenstrukturen = Objektinstanzen von Klassen Vergleich und Resümee. Operation 1.
E N D
Datenstrukturen Programmierung II Prof. Dr. Michael Löwe
Inhalt Der Begriff „Datenstruktur“ Datenstrukturen = Datentyp + Prozeduren Funktional Mit Seiteneffekte Datenstrukturen = Programm-Modul Datenstrukturen = Objektinstanzen von Klassen Vergleich und Resümee Datenstrukturen
Operation 1 Operation 2 Operation 3 Datenstrukturen Benutzer Struktur erhaltende Operationen = Schnittstelle Eigenschaften Operation 1 Operation 2 Operation 3 Rumpf 1 Rumpf 2 Rumpf 3 Konsistenz Invarianten Strukturierte Daten Datenstrukturen
Funktionskopf Parameter 1: Type Parameter 2:Type Ergebnis: Type Parameter n: Type WAS Funktionsrumpf Elem1 : type 1 Elem2 : Type 2 Elem n : type n Algorithmus Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n WIE Datentyp und Funktion Datenstrukturen
Funktionsaufruf Variablen- deklaration Funktion 1 Funktion 2 Funktion 3 Funktion n Rumpf 3 Rumpf n Rumpf 1 Rumpf 2 Name Feldselektion Feldzuweisung Datentyp Modul Feldselektion Feldzuweisung Datenstruktur = Datentyp + Funktionen Benutzer Datenstrukturen
append STREAM als funktionale Datenstruktur first removeFirst Public Type MyStream data() As Variant first As Integer behindLast As Integer size As Integer End Type Public Function first(x As MyStream) As Variant Public Function append(x As MyStream, y As Variant) As MyStream Public Sub removeFirst(x As MyStream) As MyStream Public Function emptyStream() As MyStream Public Function isEmptyStream(x As MyStream) As Boolean Public Function streamLength(x As MyStream) As Integer Datenstrukturen
1 first behindLast size d1 d2 First und emptyStream Public Function first(x As MyStream) As Variant first = x.data(x.first) End Function Public Function emptyStream() As MyStream Dim result As MyStream ReDim result.data(1 To packageSize) result.first = 1 result.behindLast = 1 result.size = packageSize emptyStream = result End Function Datenstrukturen
1 first behindLast size d1 d2 append Public Function append(x As MyStream, y As Variant) As MyStream append = x If append.behindLast <= append.size Then append.data(append.behindLast) = y append.behindLast = append.behindLast + 1 Else append.size = append.size + packageSize ReDim Preserve append.data(1 To append.size) append.data(append.behindLast) = y append.behindLast = append.behindLast + 1 End If End Function Datenstrukturen
1 first behindLast size d1 d2 removeFirst Public Function removeFirst(ByRef x As MyStream) As MyStream Dim index As Integer removeFirst = x If Not isEmptyStream(removeFirst) Then If removeFirst.first <= packageSize Then removeFirst.first = removeFirst.first + 1 Else For index = 1 To removeFirst.behindLast - packageSize - 1 removeFirst.data(index) = removeFirst.data(index + packageSize + 1) Next index removeFirst.behindLast = removeFirst.behindLast - packageSize - 1 removeFirst.first = 1 End If End If End Function Datenstrukturen
1 first behindLast size d1 d2 Benutzung (......) Dim x as MyStream Dim y as MyStream x = emptyStream x = append(x, 7) y = removeFirst(x) y = x (......) x.size = 44 x.data(12) = 18 (......) Variablen- deklaration Initialisierung Funktions- aufrufe Feldselektion Feldaktualisierung Datenstrukturen
Benutzung in Algorithmen Public Function join (x As MyStream, y As MyStream) As MyStream `Hängt den Strom y hinten an den Strom x an. Keine Seiteneffekte. Dim y1 As MyStream join = x y1 = y Do While not isemptyStream(y1) join = append(join, first(y1) y1 = removeFirst(y1) Loop End Function Datenstrukturen
Prozedurkopf Parameter 1: Type Parameter 2:Type ByRef Parameter n: Type WAS Prozedurrumpf Elem1 : type 1 Elem2 : Type 2 Elem n : type n Algorithmus Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n WIE Datentyp und Prozedur Datenstrukturen
Prozeduraufruf Variablen- deklaration Prozedur 1 Prozedur 2 Prozedur 3 Prozedur n Rumpf 2 Rumpf 3 Rumpf n Rumpf 1 Name Datentyp Modul Feldselektion Feldzuweisung Datenstruktur = Datentyp + Prozeduren Benutzer Feldselektion Feldzuweisung Datenstrukturen
STREAM als Datenstruktur Public Type MyStream data() As Variant first As Integer behindLast As Integer size As Integer End Type Public Function first(x As MyStream) As Variant Public Sub append(ByRef x As MyStream, y As Variant) Public Sub removeFirst(ByRef x As MyStream) Public Function emptyStream() As MyStream Public Function isEmptyStream(x As MyStream) As Boolean Public Function streamLength(x As MyStream) As Integer Datenstrukturen
1 first behindLast size d1 d2 append Public Sub append(ByRef x As MyStream, y As Variant) If x.behindLast <= x.size Then x.data(x.behindLast) = y x.behindLast = x.behindLast + 1 Else x.size = x.size + packageSize ReDim Preserve x.data(1 To x.size) x.data(x.behindLast) = y x.behindLast = x.behindLast + 1 End If End Sub Datenstrukturen
1 first behindLast size d1 d2 removeFirst Public Sub removeFirst(ByRef x As MyStream) Dim index As Integer If Not isEmptyStream(x) Then If x.first <= packageSize Then x.first = x.first + 1 Else For index = 1 To x.behindLast - packageSize - 1 x.data(index) = x.data(index + packageSize + 1) Next index x.behindLast = x.behindLast - packageSize - 1 x.first = 1 End If End If End Sub Datenstrukturen
1 first behindLast size d1 d2 Benutzung (......) Dim x as MyStream Dim y as MyStream x = emptyStream Call append(x, 7) Call removeFirst(x) y = x (......) x.size = 44 x.data(12) = 18 (......) Variablen- deklaration Initialisierung Funktions- aufrufe Feldselektion Feldaktualisierung Datenstrukturen
Benutzung in Algorithmen Public Sub split (ByRef x As MyStream, ByRef y As MyStream) `Leitet jedes zweite Element in x nach y um. Dim a As MyStream a = x x = emptyStream Do While not isEmptyStream(a) Call append(x, first(a)) Call removeFirst(a) If not isEmptyStream(a) Then Call append(y, first(a)) Call removeFirst(a) End If Loop End Sub Datenstrukturen
Datenstruktur = Modul + Prozeduren Prozedurkopf Parameter 1 Parameter 2 Parameter n Ergebnis Algorithmus Prozedurrumpf kontrollierte Seiteneffekte (Private) Variablen des Moduls Datenstrukturen
Prozeduraufruf Funktionsaufruf Prozedur 1 Funktion 3 Rumpf 1 Rumpf 3 Prozedur 2 Funktion n Rumpf 2 Rumpf n Auswertung Zuweisung „Private“ Variablen Modul Datenstruktur = Modul + Prozeduren Benutzer Datenstrukturen
STREAM als Modul Module MyStream Private data() As Variant Private first As Integer Private behindLast As Integer Private size As Integer Public Function first() As Variant Public Sub append(y As Variant) Public Sub removeFirst() Public Sub initialzeAsEmptyStream() Public Function isEmptyStream() As Boolean Public Function streamLength() As Integer End Module MyStream Datenstrukturen
1 first behindLast size d1 d2 append Public Sub append(y As Variant) If behindLast <= size Then data(behindLast) = y behindLast = behindLast + 1 Else size = size + packageSize ReDim Preserve data(1 To size) data(behindLast) = y behindLast = behindLast + 1 End If End Sub Datenstrukturen
1 first behindLast size d1 d2 removeFirst Public Sub removeFirst Dim index As Integer If Not isEmptyStream Then If first <= packageSize Then first = first + 1 Else For index = 1 To behindLast - packageSize - 1 data(index) = data(index + packageSize + 1) Next index behindLast = behindLast - packageSize - 1 first = 1 End If End If End Sub Datenstrukturen
1 first behindLast size d1 d2 Benutzung (......) call initialzeAsEmptyStream Call append(7) call append(12) x = first Call removeFirst (......) Initialisierung Prozedur- aufrufe Datenstrukturen
Benutzung in Algorithmen Public Sub inStream (s As String) `Liefert den Strom ab, der zusätzlich die in s durch „|“ getrennten Elemente enthält. Benutzt: Function instr(s As String, p As String) As Integer Do While s <> „“ append(left(s, instr(s, „|“)-1) s = right(s, Len(s) - instr(s, „|“)) Loop If s <> „“ Then append(s) End Sub Datenstrukturen
Funktionen und Objekte Prozedurkopf Parameter 1 Parameter 2 Parameter n Ergebnis Algorithmus Prozedurrumpf kontrollierte Seiteneffekte (Private) Variablen eines Objekts Datenstrukturen
Prozeduraufruf Funktionsaufruf Prozedur 1 Funktion 3 Rumpf 1 Rumpf 3 Prozedur 2 Funktion n Rumpf 2 Rumpf n Auswertung Zuweisung „Private“ Variablen eines Objekts Objekt Datenstruktur = Objekt zu einer Klasse Benutzer Datenstrukturen
STREAM als Klassenmodul Class MyStream Private data() As Variant Private first As Integer Private behindLast As Integer Private size As Integer Public Function first As Variant Public Sub append(y As Variant) Public Sub removeFirst Public Function isEmptyStream As Boolean Public Function streamLength As Integer Private Sub initialize End Class Datenstrukturen
1 first behindLast size d1 d2 first Public Function first As Variant first = data(first) End Function Datenstrukturen
1 first behindLast size d1 d2 append Public Sub append(y As Variant) If behindLast <= size Then data(behindLast) = y behindLast = behindLast + 1 Else size = size + packageSize ReDim Preserve data(1 To size) data(behindLast) = y behindLast = behindLast + 1 End If End Sub Datenstrukturen
1 first behindLast size d1 d2 removeFirst Public Sub removeFirst Dim index As Integer If Not isEmptyStream Then If first <= packageSize Then first = first + 1 Else For index = 1 To behindLast - packageSize - 1 data(index) = data(index + packageSize + 1) Next index behindLast = behindLast - packageSize - 1 first = 1 End If End If End Sub Datenstrukturen
1 first behindLast size d1 d2 isEmptyStream und streamLength Public Function isEmptyStream() As Boolean If first = behindLast Then isEmptyStream = True Else isEmptyStream = False End If End Function Public Function streamLength() As Integer streamLength = behindLast - first End Function Datenstrukturen
1 first behindLast size d1 d2 emptyStream:= initialize Private Sub initialize ReDim data(1 To packageSize) first = 1 behindLast = 1 size = packageSize End Sub Datenstrukturen
1 first behindLast size d1 d2 Benutzung (......) Dim x as MyStream Dim y as MyStream Set x = New MyStream Call x.append (7) Call x.append (11) Call x.removeFirst z = x.first Set y = New MyStream Set y = x Variablen- deklaration Initialisierung Funktions- aufrufe Zuweisungen Datenstrukturen
Benutzung in Algorithmen Public Function shuffle (s As MyStream) `Liefert den Strom ab, der alternierend Zeichen aus sich selbst und aus s enthält. Seiteneffekt auf s! Dim result As MyStream Set result = New MyStream Do While not (s.isEmptyStream And Me.isEmptyStream) If Not Me.isEmptyStream Then Call result.append (me.first) Call Me.removeFirst End If If Not s.isEmptyStream Then Call result.append (s.first) Call s.removeFirst End If Loop Set Me = result `(?) End Function Datenstrukturen
Vergleich und Resümee Datenstrukturen
Klasse MyCardinal Private data() As Integer Public Sub setData(x() As Integer) Public Function getData() As Integer() Public Function asCardinal(s As String) As MyCardinal Public Function asString() As String Public Function cNull() As MyCardinal Public Function cOne() As MyCardinal Public Function cAdd(x As MyCardinal) As MyCardinal Public Function cSub(x As MyCardinal) As MyCardinal Public Function cMul(x As MyCardinal) As MyCardinal Public Function cDiv(x As MyCardinal) As MyCardinal Public Function cMod(x As MyCardinal) As MyCardinal Public Function cEQ(x As MyCardinal) As Boolean Public Function cBG(x As MyCardinal) As Boolean Datenstrukturen
Klasse MyInteger Private positive As MyCardinal Private negative As MyCardinal Public Sub setPositive(x As MyCardinal) Public Sub setNegative(x As MyCardinal) Public Function getPositive As MyCardinal Public Function getNegative As MyCardinal Public Function asInteger(s As String) As MyInteger Public Function asString() As String Public Function iNull() As MyInteger Public Function iOne() As MyIntegerl Public Function iAdd(x As MyInteger) As MyInteger Public Function iSub......... Public Function iEQ(x As MyInteger) As Boolean Public Function iBG(x As MyInteger) As Boolean Datenstrukturen
Klasse MyRational Private numerator As MyInteger Private denominator As MyInteger Public Sub setNumerator(x As MyInteger) Public Sub setDenominator(x As MyInteger) Public Function getNumerator As MyInteger Public Function getdenominator As MyInteger Public Function asRational(s As String) As MyRational Public Function asString() As String Public Function rNull() As MyRational Public Function rOne() As MyRationall Public Function rAdd(x As MyRational) As MyRational Public Function rSub......... Public Function rEQ(x As MyRational) As Boolean Public Function rBG(x As MyRational) As Boolean Datenstrukturen
Beispiele für Implementierungen Public Function rMul(x As MyRational) As MyRational Set rMul = New MyRational Call rMul.setNumerator (x.getNumerator.iMul (numerator)) Call rMul.setDenominator (x.getDenominator.iMul (denominator)) End Function Public Function iAdd(x As MyInteger) As MyInteger Set iAdd = New MyInteger Call iAdd.setPositive (x.getPositive.cAdd (positive)) Call iAdd.setNegative (x.getNegative.cAdd (negative)) End Function Datenstrukturen
Beispiele für Implementierungen Public Function rAdd(x As MyRational) As MyRational Set rAdd = New MyRational Call rMul.setNumerator (x.getNumerator.iMul (denominator).iAdd (x.getDenominator.iMul (numerator))) Call rMul.setDenominator (x.getDenominator.iMul (denominator)) End Function Datenstrukturen