1
0
Fork 0

Updated examples

master
Thomas Smits 2023-02-18 15:46:46 +01:00
parent 75e72c3adf
commit 7af662d37c
532 changed files with 7648 additions and 199 deletions

19
README.md 100644
View File

@ -0,0 +1,19 @@
# Beispiele zur Vorlesung PR2
Hier finden Sie die Beispiele aus den Folien (und dem Skript) zur Vorlesung PR2.
* [01_auffrischung](src/main/java/pr2/auffrischung)
* [02_intro](src/main/java/pr2/intro)
* [03_strukturierung](src/main/java/pr2/strukturierung)
* [04_oo](src/main/java/pr2/oo)
* [05_algorithmen](src/main/java/pr2/algorithmen)
* [07_exceptions](src/main/java/pr2/exceptions)
* [08_enumerationen](src/main/java/pr2/enumerationen)
* [09_generics](src/main/java/pr2/generics)
* [10_geschachtelte_klassen](src/main/java/pr2/geschachtelte_klassen)
* [11_datenstrukturen](src/main/java/pr2/datenstrukturen)
* [12_collections](src/main/java/pr2/collections)
* [13_streams](src/main/java/pr2/streams)
* [14_threads](src/main/java/pr2/threads)
* [15_io](src/main/java/pr2/io)
* [16_reflection](src/main/java/pr2/reflection)

View File

@ -1,4 +1,4 @@
package pr2.ads.adt; package pr2.algorithmen.adt;
public class BinaryTree { public class BinaryTree {
private TreeNode head; private TreeNode head;

View File

@ -1,4 +1,4 @@
package pr2.ads.adt; package pr2.algorithmen.adt;
public interface Queue { public interface Queue {
public void enter(Object obj) throws QueueException; public void enter(Object obj) throws QueueException;

View File

@ -1,4 +1,4 @@
package pr2.ads.adt; package pr2.algorithmen.adt;
public class QueueException extends RuntimeException { public class QueueException extends RuntimeException {
} }

View File

@ -1,4 +1,4 @@
package pr2.ads.adt; package pr2.algorithmen.adt;
public interface Stack { public interface Stack {
void push(Object obj) throws StackException; void push(Object obj) throws StackException;

View File

@ -1,4 +1,4 @@
package pr2.ads.adt; package pr2.algorithmen.adt;
public class StackException extends RuntimeException { public class StackException extends RuntimeException {
} }

View File

@ -1,4 +1,4 @@
package pr2.ads.adt; package pr2.algorithmen.adt;
public class TreeNode { public class TreeNode {
TreeNode left = null; TreeNode left = null;

View File

@ -1,4 +1,4 @@
package pr2.ads.searching; package pr2.algorithmen.searching;
public class BinarySearch implements Searcher { public class BinarySearch implements Searcher {
@Override @Override

View File

@ -1,4 +1,4 @@
package pr2.ads.searching; package pr2.algorithmen.searching;
public class LinearSearch implements Searcher { public class LinearSearch implements Searcher {
@Override @Override

View File

@ -1,4 +1,4 @@
package pr2.ads.searching; package pr2.algorithmen.searching;
public interface Searcher { public interface Searcher {

View File

@ -1,4 +1,4 @@
package pr2.ads.searching; package pr2.algorithmen.searching;
import java.util.Random; import java.util.Random;

View File

@ -1,4 +1,4 @@
package pr2.ads.shuffle; package pr2.algorithmen.shuffle;
import java.util.Random; import java.util.Random;

View File

@ -1,4 +1,4 @@
package pr2.ads.shuffle; package pr2.algorithmen.shuffle;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Random; import java.util.Random;

View File

@ -1,4 +1,4 @@
package pr2.ads.shuffle; package pr2.algorithmen.shuffle;
public interface Shuffler { public interface Shuffler {

View File

@ -1,4 +1,4 @@
package pr2.ads.shuffle; package pr2.algorithmen.shuffle;
import java.util.Arrays; import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
public class BubbleSort implements Sorter { public class BubbleSort implements Sorter {

View File

@ -1,4 +1,4 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
public class InsertionSort implements Sorter { public class InsertionSort implements Sorter {

View File

@ -1,4 +1,4 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
public class MergeSort implements Sorter { public class MergeSort implements Sorter {

View File

@ -1,4 +1,4 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
public class QuickSort implements Sorter { public class QuickSort implements Sorter {

View File

@ -1,4 +1,4 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
public class SelectionSort implements Sorter { public class SelectionSort implements Sorter {

View File

@ -1,4 +1,4 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
public interface Sorter { public interface Sorter {

View File

@ -1,6 +1,6 @@
package pr2.ads.sorting; package pr2.algorithmen.sorting;
import pr2.nestedclasses.lambda.function.RechnerKurz; import pr2.geschachtelte_klassen.lambda.function.RechnerKurz;
import java.util.*; import java.util.*;

View File

@ -1,4 +1,4 @@
package pr2.ads.textsearch; package pr2.algorithmen.textsearch;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -1,4 +1,4 @@
package pr2.ads.textsearch; package pr2.algorithmen.textsearch;
public class BruteForceTextSearch implements TextSearch { public class BruteForceTextSearch implements TextSearch {

View File

@ -1,4 +1,4 @@
package pr2.ads.textsearch; package pr2.algorithmen.textsearch;
public class KMPTextSearch implements TextSearch { public class KMPTextSearch implements TextSearch {

View File

@ -1,4 +1,4 @@
package pr2.ads.textsearch; package pr2.algorithmen.textsearch;
public class TestTextSearch { public class TestTextSearch {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -1,4 +1,4 @@
package pr2.ads.textsearch; package pr2.algorithmen.textsearch;
public interface TextSearch { public interface TextSearch {

View File

@ -0,0 +1,23 @@
package pr2.algorithmen.searching;
public class BinarySearch implements Searcher {
@Override
public int search(int e, int[] data) {
int u = 0;
int o = data.length - 1;
while (u <= o) {
int m = (u + o) / 2;
if (data[m] == e) {
return m;
}
else if (data[m] > e) {
o = m - 1;
}
else {
u = m + 1;
}
}
return NOT_FOUND;
}
}

View File

@ -0,0 +1,14 @@
package pr2.algorithmen.searching;
public class LinearSearch implements Searcher {
@Override
public int search(int e, int[] data) {
for (int i = 0; i < data.length; i++) {
if (e == data[i]) {
return i;
}
}
return NOT_FOUND;
}
}

View File

@ -0,0 +1,8 @@
package pr2.algorithmen.searching;
public interface Searcher {
int NOT_FOUND = -1;
int search(int e, int[] data);
}

View File

@ -0,0 +1,27 @@
package pr2.algorithmen.searching;
import java.util.Random;
public class TestSearch {
public static void main(String[] args) {
final int size = 1000;
var data = new int[size];
var r = new Random();
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
Searcher s;
int index;
s = new LinearSearch();
index = s.search(r.nextInt(size), data);
System.out.println(index);
s = new BinarySearch();
index = s.search(r.nextInt(size), data);
System.out.println(index);
}
}

View File

@ -0,0 +1,18 @@
package pr2.algorithmen.shuffle;
import java.util.Random;
public class FisherYatesShuffle implements Shuffler {
@Override
public void shuffle(int[] data) {
Random rand = new Random();
for (int i = data.length - 1; i > 0; i--) {
int randomIndexToSwap = rand.nextInt(data.length);
int temp = data[randomIndexToSwap];
data[randomIndexToSwap] = data[i];
data[i] = temp;
}
}
}

View File

@ -0,0 +1,28 @@
package pr2.algorithmen.shuffle;
import java.util.LinkedList;
import java.util.Random;
public class NaiveShuffle implements Shuffler {
@Override
public void shuffle(int[] data) {
Random rand = new Random();
int n = data.length;
var result = new int[n];
var index = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
index.add(i);
}
for (int i = 0; i < n; i++) {
int z = rand.nextInt(n - i);
result[i] = data[index.get(z)];
index.remove(z);
}
System.arraycopy(result, 0, data, 0, n);
}
}

View File

@ -0,0 +1,12 @@
package pr2.algorithmen.shuffle;
public interface Shuffler {
void shuffle(int[] data);
default void fill(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
}
}

View File

@ -0,0 +1,20 @@
package pr2.algorithmen.shuffle;
import java.util.Arrays;
public class TestShuffle {
public static void main(String[] args) {
Shuffler naive = new NaiveShuffle();
Shuffler fisher = new FisherYatesShuffle();
int[] data = new int[10];
naive.fill(data);
naive.shuffle(data);
System.out.println(Arrays.toString(data));
fisher.fill(data);
fisher.shuffle(data);
System.out.println(Arrays.toString(data));
}
}

View File

@ -0,0 +1,24 @@
package pr2.algorithmen.sorting;
public class BubbleSort implements Sorter {
@Override
public void sort(int[] data) {
boolean swapped;
do {
swapped = false;
for (int i = 0; i < data.length - 1; i++) {
if (data[i] > data[i + 1]) {
// Elemente vertauschen
swap(data, i, i + 1);
swapped = true;
}
}
} while (swapped);
}
@Override
public String toString() {
return "BubbleSort";
}
}

View File

@ -0,0 +1,28 @@
package pr2.algorithmen.sorting;
public class InsertionSort implements Sorter {
@Override
public void sort(int[] data) {
for (int i = 1; i < data.length; i++) {
int m = data[i];
int j = i;
while (j > 0) {
if (data[j - 1] >= m) {
/* Verschiebe data[j - 1] eine Position nach rechts */
data[j] = data[j - 1];
j--;
}
else {
break;
}
}
data[j] = m;
}
}
@Override
public String toString() {
return "InsertionSort";
}
}

View File

@ -0,0 +1,46 @@
package pr2.algorithmen.sorting;
public class MergeSort implements Sorter {
void msort(int[] array, int le, int ri, int[] helper) {
int i, j, k;
if (ri > le) {
// zu sortierendes Feld teilen
int mid = (ri + le) / 2;
// Teilfelder sortieren
msort(array, le, mid, helper);
msort(array, mid + 1, ri, helper);
// Hilfsfeld aufbauen
for (k = le; k <= mid; k++) {
helper[k] = array[k];
}
for (k = mid; k < ri; k++) {
helper[ri + mid - k] = array[k + 1];
}
// Ergebnisse mischen über Hilfsfeld
i = le; j = ri;
for (k = le; k <= ri; k++) {
if (helper[i] < helper[j]) {
array[k] = helper[i++];
} else {
array[k] = helper[j--];
}
}
}
}
@Override
public void sort(int[] data) {
int[] helper = new int[data.length];
msort(data, 0, data.length - 1, helper);
}
@Override
public String toString() {
return "MergeSort";
}
}

View File

@ -0,0 +1,47 @@
package pr2.algorithmen.sorting;
public class QuickSort implements Sorter {
int partition(int[] array, int u, int o, int p) {
int pn = u;
int pv = array[p];
// Pivot-Element an das Ende verschieben
swap(array, p, o);
for (int i = u; i < o; i++) {
if (array[i] <= pv) {
swap(array, pn++, i);
}
}
// Pivot-Element an die richtige Position kopieren
swap(array, o, pn);
// neue Pivot-Position zurückgeben
return pn;
}
// Hilfsmethode zum rekursiven Sortieren
void qsort(int[] array, int u, int o) {
// Pivot-Element bestimmen
int p = (u + o) / 2;
if (o > u) {
// Feld zerlegen
int pn = partition(array, u, o, p);
// und Partitionen sortieren
qsort(array, u, pn - 1);
qsort(array, pn + 1, o);
}
}
@Override
public void sort(int[] data) {
qsort(data, 0, data.length - 1);
}
@Override
public String toString() {
return "QuickSort";
}
}

View File

@ -0,0 +1,26 @@
package pr2.algorithmen.sorting;
public class SelectionSort implements Sorter {
@Override
public void sort(int[] data) {
int marker = data.length - 1;
while (marker >= 0) {
// bestimme größtes Element
int max = 0;
for (int i = 1; i <= marker; i++) {
if (data[i] > data[max]) {
max = i;
}
}
// tausche array[marker] mit diesem Element
swap(data, marker, max);
marker--;
}
}
@Override
public String toString() {
return "SelectionSort";
}
}

View File

@ -0,0 +1,12 @@
package pr2.algorithmen.sorting;
public interface Sorter {
void sort(int[] data);
default void swap(int[] data, int i1, int i2) {
int tmp = data[i1];
data[i1] = data[i2];
data[i2] = tmp;
}
}

View File

@ -0,0 +1,73 @@
package pr2.algorithmen.sorting;
import java.util.*;
public class TestSorting {
private static void shuffle(int[] data) {
Random rand = new Random();
for (int i = 0; i < data.length; i++) {
int randomIndexToSwap = rand.nextInt(data.length);
int temp = data[randomIndexToSwap];
data[randomIndexToSwap] = data[i];
data[i] = temp;
}
}
static long testAlgorithm(Class clazz, int size, int runs) {
Sorter s;
var data = new int[size];
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
long start = System.nanoTime();
try {
s = (Sorter) clazz.getDeclaredConstructor().newInstance();
for (int i = 0; i < runs; i++) {
shuffle(data);
s.sort(data);
}
}
catch (Exception e) {
// do nothing
return 0;
}
long runtime = System.nanoTime() - start;
return runtime;
}
public static void measure(int size_steps, int max_size, int runs) {
System.out.println("Runs\tBS\tIS\tMS\tQS\tSS");
for (int size = 0; size <= max_size; size += size_steps) {
long bs = testAlgorithm(BubbleSort.class, size, runs);
long is = testAlgorithm(InsertionSort.class, size, runs);
long ms = testAlgorithm(MergeSort.class, size, runs);
long qs = testAlgorithm(QuickSort.class, size, runs);
long ss = testAlgorithm(SelectionSort.class, size, runs);
System.out.printf("%d\t%d\t%d\t%d\t%d\t%d\n", size, bs, is, ms, qs, ss);
}
}
public static void main(String[] args) {
measure(10, 1000, 100);
final int size = 1000;
final int runs = 1000;
testAlgorithm(BubbleSort.class, size, runs);
testAlgorithm(InsertionSort.class, size, runs);
testAlgorithm(MergeSort.class, size, runs);
testAlgorithm(QuickSort.class, size, runs);
testAlgorithm(SelectionSort.class, size, runs);
}
}

View File

@ -0,0 +1,49 @@
package pr2.algorithmen.textsearch;
import java.util.HashMap;
import java.util.Map;
public class BoyerMooreTextSearch implements TextSearch {
public int search(String haystack, String needle) {
char[] text = haystack.toCharArray();
char[] pattern = needle.toCharArray();
int n = text.length;
int m = pattern.length;
// Test for empty string
if (m == 0) return 0;
// Initialization, create Map of last position of each character = O(n)
Map<Character, Integer> last = new HashMap<>();
for (char c : text) {
// set all chars, by default, to -1
last.put(c, -1);
}
for (int i = 0; i < m; i++) {
// update last seen positions
last.put(pattern[i], i);
}
//Start with the end of the pattern aligned at index m-1 in the text.
//index into the text
int i = m - 1;
// index into the pattern
int k = m - 1;
while (i < n) {
if (text[i] == pattern[k]) {
// match! return i if complete match; otherwise, keep checking
if (k == 0) {
return i;
}
i--; k--;
}
else { // jump step + restart at end of pattern
//iterate over text
i += m - Math.min(k, 1 + last.get(text[i]));
//move to end of pattern
k = m - 1;
}
}
// not found
return NOT_FOUND;
}
}

View File

@ -0,0 +1,22 @@
package pr2.algorithmen.textsearch;
public class BruteForceTextSearch implements TextSearch {
public int search(String haystack, String needle) {
int n = haystack.length();
int m = needle.length();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
break;
}
}
if (j == m) {
return i;
}
}
return NOT_FOUND;
}
}

View File

@ -0,0 +1,46 @@
package pr2.algorithmen.textsearch;
public class KMPTextSearch implements TextSearch {
public int search(String haystack, String needle) {
int[] lps = computeLPS(needle);
int i = 0, j = 0;
while (i < haystack.length()) {
if (needle.charAt(j) == haystack.charAt(i)) {
i++;
j++;
}
if (j == needle.length()) {
return i - j;
} else if (i < haystack.length() && needle.charAt(j) != haystack.charAt(i)) {
if (j != 0) {
j = lps[j - 1];
} else {
i = i + 1;
}
}
}
return -1;
}
public int[] computeLPS(String pattern) {
int[] lps = new int[pattern.length()];
int j = 0;
for (int i = 1; i < pattern.length();) {
if (pattern.charAt(i) == pattern.charAt(j)) {
lps[i] = j + 1;
i++;
j++;
} else {
if (j != 0) {
j = lps[j - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
}

View File

@ -0,0 +1,21 @@
package pr2.algorithmen.textsearch;
public class TestTextSearch {
public static void main(String[] args) {
String haystack = "DASISTEINSINNLOSERTEXT";
String needle = "SINN";
TextSearch bf = new BruteForceTextSearch();
System.out.println(bf.search(haystack, needle));
TextSearch kmp = new KMPTextSearch();
System.out.println(kmp.search(haystack, needle));
TextSearch bm = new BoyerMooreTextSearch();
System.out.println(bm.search(haystack, needle));
}
}

View File

@ -0,0 +1,8 @@
package pr2.algorithmen.textsearch;
public interface TextSearch {
int NOT_FOUND = -1;
int search(String haystack, String needle);
}

View File

@ -1,5 +1,5 @@
/* (c) 2022 Thomas Smits */ /* (c) 2022 Thomas Smits */
package pr2.ausnahmen; package pr2.exceptions;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.callstack; package pr2.exceptions.callstack;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.callstack; package pr2.exceptions.callstack;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.callstack; package pr2.exceptions.callstack;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;

View File

@ -1,5 +1,5 @@
package pr2.ausnahmen.callstack; package pr2.exceptions.callstack;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.eigene; package pr2.exceptions.eigene;
public class Client { public class Client {

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.eigene; package pr2.exceptions.eigene;
public class Server { public class Server {

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.eigene; package pr2.exceptions.eigene;
public class ServerException extends Exception { public class ServerException extends Exception {

View File

@ -1,5 +1,5 @@
/* (c) 2022 Thomas Smits */ /* (c) 2022 Thomas Smits */
package pr2.ausnahmen.eigene; package pr2.exceptions.eigene;
import java.io.IOException; import java.io.IOException;

View File

@ -1,5 +1,5 @@
/* (c) 2022 Thomas Smits */ /* (c) 2022 Thomas Smits */
package pr2.ausnahmen.eigene; package pr2.exceptions.eigene;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.finall; package pr2.exceptions.finall;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.finall; package pr2.exceptions.finall;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.junit; package pr2.exceptions.junit;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;

View File

@ -1,4 +1,4 @@
package pr2.ausnahmen.junit; package pr2.exceptions.junit;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.junit; package pr2.exceptions.junit;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.quiz; package pr2.exceptions.quiz;
public class Finally { public class Finally {

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
public class ExceptionExample { public class ExceptionExample {

View File

@ -1,5 +1,5 @@
/* (c) 2022 Thomas Smits */ /* (c) 2022 Thomas Smits */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
public class FinallyMitSeiteneffekt { public class FinallyMitSeiteneffekt {

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
public class GanzBoese { public class GanzBoese {

View File

@ -1,5 +1,5 @@
/* (c) 2022 Thomas Smits */ /* (c) 2022 Thomas Smits */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch; package pr2.exceptions.trycatch;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch.polymorphie; package pr2.exceptions.trycatch.polymorphie;
import java.io.IOException; import java.io.IOException;

View File

@ -1,7 +1,7 @@
/* /*
* (c) 2009 Thomas Smits * (c) 2009 Thomas Smits
*/ */
package pr2.ausnahmen.trycatch.polymorphie; package pr2.exceptions.trycatch.polymorphie;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;

View File

@ -0,0 +1,14 @@
package pr2.datenstrukturen.adt;
public class BinaryTree {
private TreeNode head;
private TreeNode nullNode;
public BinaryTree() {
head = new TreeNode(null);
nullNode = new TreeNode(null);
head.setRight(nullNode);
nullNode.setLeft(nullNode);
nullNode.setRight(nullNode);
}
}

View File

@ -0,0 +1,8 @@
package pr2.datenstrukturen.adt;
public interface Queue {
public void enter(Object obj) throws QueueException;
public Object leave() throws QueueException;
public Object front() throws QueueException;
public boolean isEmpty();
}

View File

@ -0,0 +1,4 @@
package pr2.datenstrukturen.adt;
public class QueueException extends RuntimeException {
}

View File

@ -0,0 +1,8 @@
package pr2.datenstrukturen.adt;
public interface Stack {
void push(Object obj) throws StackException;
Object pop() throws StackException;
Object top() throws StackException;
boolean isEmpty();
}

View File

@ -0,0 +1,4 @@
package pr2.datenstrukturen.adt;
public class StackException extends RuntimeException {
}

View File

@ -0,0 +1,22 @@
package pr2.datenstrukturen.adt;
public class TreeNode {
TreeNode left = null;
TreeNode right = null;
Object key;
public TreeNode(Object o) { key = o; }
public Object getKey() { return key; }
public TreeNode getLeft() { return left; }
public TreeNode getRight() { return right; }
public void setLeft(TreeNode n) { left = n; }
public void setRight(TreeNode n) { right = n; }
public String toString() { return key.toString (); }
}

View File

@ -0,0 +1,118 @@
package pr2.datenstrukturen.avl;
class Node {
int key, height;
Node left, right;
public Node(int key) {
this.key = key;
height = 1;
}
}
public class AVLTree {
Node root;
int height(Node N) {
if (N == null)
return 0;
return N.height;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;
x.right = y;
y.left = T2;
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
return x;
}
Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
y.left = x;
x.right = T2;
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
return y;
}
int getBalance(Node N) {
if (N == null)
return 0;
return height(N.left) - height(N.right);
}
Node insert(Node node, int key) {
if (node == null)
return (new Node(key));
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
else
return node;
node.height = 1 + max(height(node.left),
height(node.right));
int balance = getBalance(node);
if (balance > 1 && key < node.left.key)
return rightRotate(node);
if (balance < -1 && key > node.right.key)
return leftRotate(node);
if (balance > 1 && key > node.left.key) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
if (balance < -1 && key < node.right.key) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
void preOrder(Node node) {
if (node != null) {
System.out.print(node.key + " ");
preOrder(node.left);
preOrder(node.right);
}
}
public static void main(String[] args) {
AVLTree tree = new AVLTree();
/* Constructing tree given in the above figure */
tree.root = tree.insert(tree.root, 10);
tree.root = tree.insert(tree.root, 20);
tree.root = tree.insert(tree.root, 30);
tree.root = tree.insert(tree.root, 40);
tree.root = tree.insert(tree.root, 50);
tree.root = tree.insert(tree.root, 25);
System.out.println("Preorder traversal" +
" of constructed tree is : ");
tree.preOrder(tree.root);
}
}

View File

@ -0,0 +1,66 @@
package pr2.datenstrukturen.bt;
class Node {
int data;
Node left;
Node right;
public Node(int item) {
data = item;
left = right = null;
}
}
public class BinaryTree {
// Wurzel des Baums
Node root;
public BinaryTree() {
root = null;
}
public void insert(int key) {
root = insertRec(root, key);
}
private Node insertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.data)
root.left = insertRec(root.left, key);
else if (key > root.data)
root.right = insertRec(root.right, key);
return root;
}
public void inorder() {
inorderRec(root);
}
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.data);
inorderRec(root.right);
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.inorder();
}
}

View File

@ -0,0 +1,94 @@
package pr2.datenstrukturen.bt;
import java.util.LinkedList;
import java.util.function.Consumer;
public class SimpleTree {
private static class Node {
private Node left;
private Node right;
private String data;
public Node(String data, Node left, Node right) {
this.left = left;
this.right = right;
this.data = data;
}
public Node(String data) {
this.left = null;
this.right = null;
this.data = data;
}
}
private Node root;
public SimpleTree(Node root) {
this.root = root;
}
public void preorder(Consumer<String> f) {
preorder(f, this.root);
}
public void preorder(Consumer<String> f, Node node) {
if (node == null) return;
f.accept(node.data); // Aktion auf dem Knoten ausführen
preorder(f, node.left); // Linker Teilbaum
preorder(f, node.right); // Rechter Teilbaum
}
public void inorder(Consumer<String> f) {
inorder(f, this.root);
}
public void inorder(Consumer<String> f, Node node) {
if (node == null) return;
preorder(f, node.left); // Linker Teilbaum
f.accept(node.data); // Aktion auf dem Knoten ausführen
preorder(f, node.right); // Rechter Teilbaum
}
public void postorder(Consumer<String> f) {
postorder(f, this.root);
}
public void postorder(Consumer<String> f, Node node) {
if (node == null) return;
postorder(f, node.left); // Linker Teilbaum
postorder(f, node.right); // Rechter Teilbaum
f.accept(node.data); // Aktion auf dem Knoten ausführen
}
public void levelorder(Consumer<String> f) {
var list = new LinkedList<Node>();
list.add(root);
while (!list.isEmpty()) {
Node n = list.pollFirst();
f.accept(n.data); // Aktion ausführen
if (n.left != null) {
list.add(n.left);
}
if (n.right != null) {
list.add(n.right);
}
}
}
public static void main(String[] args) {
SimpleTree t = new SimpleTree(
new Node("A",
new Node("B", new Node("D"), new Node("E")),
new Node("C", new Node("F"), new Node("G"))));
System.out.print("Pre-Order: ");
t.preorder(System.out::print);
System.out.print("\nIn-Order: ");
t.inorder(System.out::print);
System.out.print("\nPost-Order: ");
t.postorder(System.out::print);
System.out.print("\nLevel-Order: ");
t.levelorder(System.out::print);
}
}

View File

@ -0,0 +1,55 @@
package pr2.datenstrukturen.buffer;
public class RingBuffer<T> {
private T[] elements;
private int head = 0;
private int tail = 0;
private int size = 0;
public RingBuffer(int capacity) {
elements = (T[]) new Object[capacity];
}
// Element hinzufügen
public void add(T element) {
if (size == elements.length) {
head = (head + 1) % elements.length;
} else {
size++;
}
elements[tail] = element;
tail = (tail + 1) % elements.length;
}
// erstes Element zurückgeben
public T first() {
if (size == 0) {
return null;
}
return elements[head];
}
// letztes Element zurückgeben
public T last() {
if (size == 0) {
return null;
}
return elements[(tail - 1 + elements.length) % elements.length];
}
// Puffer leer?
public boolean isEmpty() {
return size == 0;
}
// Puffer voll?
public boolean isFull() {
return size == elements.length;
}
// Anzahl der Elemente im Puffer
public int size() {
return size;
}
}

View File

@ -0,0 +1,54 @@
package pr2.datenstrukturen.graph;
import java.util.LinkedList;
class Graph {
private int V; // Anzahl der Knoten
private LinkedList<Integer> adj[]; // Adjazenzliste
Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
}
void DFS(int v, boolean visited[]) {
visited[v] = true;
System.out.print(v + " ");
for (Integer neighbour : adj[v]) {
if (!visited[neighbour])
DFS(neighbour, visited);
}
}
void DFS() {
boolean visited[] = new boolean[V];
for (int i = 0; i < V; i++) {
if (!visited[i]) {
DFS(i, visited);
}
}
}
public static void main(String args[]) {
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Depth First Traversal");
g.DFS();
}
}

View File

@ -0,0 +1,53 @@
package pr2.datenstrukturen.hash;
import java.util.LinkedList;
public class BetterHashTable {
private static final int SIZE = 10;
private final LinkedList[] buckets = new LinkedList[SIZE];
public BetterHashTable() {
for (int i = 0; i < SIZE; i++) {
buckets[i] = new LinkedList();
}
}
public int idx(String s) {
return Math.abs(s.hashCode()) % SIZE;
}
public void add(String s) {
buckets[idx(s)].add(s);
}
public void remove(String s) {
buckets[idx(s)].remove(s);
}
public boolean contains(String s) {
return buckets[idx(s)].contains(s);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SIZE; i++) {
sb.append(String.format("[%d]: %s\n", i,
buckets[i] == null ? "" : buckets[i]));
}
return sb.toString();
}
public static void main(String[] args) {
BetterHashTable sh = new BetterHashTable();
sh.add("Januar");
sh.add("Februar");
sh.add("März");
sh.add("April");
sh.add("Mai");
System.out.println(sh.toString());
System.out.println(sh.contains("Februar")); // -> true
System.out.println(sh.contains("Januar")); // -> true
}
}

View File

@ -0,0 +1,51 @@
package pr2.datenstrukturen.hash;
import java.util.LinkedList;
public class HashTable {
private int SIZE = 10;
private LinkedList<Node>[] table;
HashTable() {
table = new LinkedList[SIZE];
for (int i = 0; i < SIZE; i++)
table[i] = new LinkedList();
}
public void put(int key, int value) {
int index = hash(key);
table[index].add(new Node(key, value));
}
public int get(int key) {
int index = hash(key);
for (Node node : table[index])
if (node.key == key)
return node.value;
return -1;
}
public void remove(int key) {
int index = hash(key);
for (Node node : table[index])
if (node.key == key) {
table[index].remove(node);
break;
}
}
private int hash(int key) {
return key % SIZE;
}
private static class Node {
int key;
int value;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
}

View File

@ -0,0 +1,45 @@
package pr2.datenstrukturen.hash;
public class SimpleHashTable {
private static final int SIZE = 10;
private final String[] buckets = new String[SIZE];
public int idx(String s) {
return Math.abs(s.hashCode()) % SIZE;
}
public void add(String s) {
buckets[idx(s)] = s;
}
public void remove(String s) {
buckets[idx(s)] = null;
}
public boolean contains(String s) {
return s.equals(buckets[idx(s)]);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SIZE; i++) {
sb.append(String.format("[%d]: %s\n", i,
buckets[i] == null ? "" : buckets[i]));
}
return sb.toString();
}
public static void main(String[] args) {
SimpleHashTable sh = new SimpleHashTable();
sh.add("Januar");
sh.add("Februar");
sh.add("März");
sh.add("April");
sh.add("Mai");
System.out.println(sh.toString());
System.out.println(sh.contains("Februar"));
System.out.println(sh.contains("Januar"));
}
}

View File

@ -0,0 +1,24 @@
package pr2.datenstrukturen.liste;
public class ArrayIterator {
private final int[] array;
private int pos;
public ArrayIterator(int[] array) {
this.array = array.clone();
pos = 0;
}
public boolean hasNext() {
return pos < array.length;
}
public int next() {
return array[pos++]; // ArrayIndexOutOfBoundsException
}
public void reset() {
pos = 0;
}
}

View File

@ -0,0 +1,14 @@
package pr2.datenstrukturen.liste;
public class ArrayIteratorTest {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
ArrayIterator ai1 = new ArrayIterator(array);
ArrayIterator ai2 = new ArrayIterator(array);
while (ai1.hasNext()) {
System.out.println(ai1.next());
}
}
}

View File

@ -0,0 +1,112 @@
package pr2.datenstrukturen.liste;
class DoubleLinkedList implements IntList {
private static class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Node head;
Node tail;
public void append(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
return;
}
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
public void prepend(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
return;
}
newNode.next = head;
head.prev = newNode;
head = newNode;
}
public void deleteWithValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
head.prev = null;
return;
}
Node current = head;
while (current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
if (current.next != null) {
current.next.prev = current;
}
return;
}
current = current.next;
}
}
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[ ");
Node current = head;
while (current != null) {
sb.append(current.data);
current = current.next;
if (current != null) {
sb.append(", ");
}
}
sb.append(" ]");
return sb.toString();
}
public int size() {
Node current = head;
int count = 0;
while (current != null) {
count++;
current = current.next;
}
return count;
}
}

View File

@ -0,0 +1,9 @@
package pr2.datenstrukturen.liste;
public interface IntList {
void append(int data);
void prepend(int data);
int size();
}

View File

@ -0,0 +1,120 @@
package pr2.datenstrukturen.liste;
class LinkedList implements IntList {
private static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
Node head;
private Node last() {
Node result = head;
while (result != null && result.next != null) {
result = result.next;
}
return result;
}
public void append(int data) {
Node newNode = new Node(data);
Node last = last();
if (last == null) {
head = newNode;
}
else {
last.next = newNode;
}
}
public void prepend(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
public int size() {
Node current = head;
int count = 0;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public int remove(int index) {
Node current = head;
Node prev = head;
if (current == null) {
throw new RuntimeException("List is empty");
}
if (index >= size()) {
throw new IndexOutOfBoundsException();
}
int count = 0;
while (current != null) {
if (index == count) {
int data = current.data;
prev.next = current.next;
return data;
}
prev = current;
current = current.next;
}
return count;
}
public void delete(int data) {
Node current = head, prev = null;
if (current != null && current.data == data) {
head = current.next;
return;
}
while (current != null && current.data != data) {
prev = current;
current = current.next;
}
if (current == null)
return;
prev.next = current.next;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[ ");
Node current = head;
while (current != null) {
sb.append(current.data);
current = current.next;
if (current != null) {
sb.append(", ");
}
}
sb.append(" ]");
return sb.toString();
}
}

View File

@ -0,0 +1,168 @@
package pr2.datenstrukturen.liste;
public class Pr2LinkedList<T> implements Pr2List<T> {
/** Start node. */
private Pr2Node<T> first;
/** Last Node. */
private Pr2Node<T> last;
@Override
public void add(T e) {
if (first == null) {
first = new Pr2Node<>(e);
last = first;
}
else {
last.next = new Pr2Node<>(e);
last = last.next;
}
}
private Pr2Node<T> at(int index) {
if (index == 0 && first != null) {
return first;
}
Pr2Node<T> n = first;
int count = 0;
while (n != null) {
if (count == index) {
return n;
}
n = n.next;
count++;
}
throw new ArrayIndexOutOfBoundsException();
}
@Override
public T get(int index) {
// no negative indices
if (index < 0) {
throw new ArrayIndexOutOfBoundsException("Negative index");
}
return at(index).element;
}
@Override
public T remove(int index) {
T e;
// no negative indices
if (index < 0) {
throw new ArrayIndexOutOfBoundsException("Negative index");
}
// Case first node
if (index == 0 && first != null) {
if (last == first) {
last = null;
}
e = first.element;
first = first.next;
return e;
}
Pr2Node<T> prevNode = at(index - 1);
if (prevNode.next == null) {
// index not present
throw new ArrayIndexOutOfBoundsException();
}
e = prevNode.next.element;
prevNode.next = prevNode.next.next;
if (prevNode.next == null) {
last = prevNode;
}
return e;
}
@Override
public int length() {
Pr2Node<T> n = first;
int count = 0;
while (n != null) {
n = n.next;
count++;
}
return count;
}
@Override
public T removeLast() {
T e;
if (last == null) {
throw new ArrayIndexOutOfBoundsException("Empty List");
}
if (first == last) {
e = last.element;
first = null;
last = null;
}
else {
Pr2Node<T> n = first;
while (n.next != last) {
n = n.next;
}
e = n.next.element;
n.next = null;
last = n;
}
return e;
}
@Override
public T removeFirst() {
if (last == null) {
throw new ArrayIndexOutOfBoundsException("Empty List");
}
T e = first.element;
if (first == last) {
first = null;
last = null;
}
else {
first = first.next;
}
return e;
}
@Override
public void free() {
first = null;
last = null;
}
/**
* @see Object#toString()
*/
@Override
public String toString() {
StringBuilder b = new StringBuilder("[ ");
Pr2Node<T> n = first;
while (n != null) {
b.append(n.element);
b.append(", ");
n = n.next;
}
b.append(']');
return b.toString();
}
}

View File

@ -0,0 +1,124 @@
package pr2.datenstrukturen.liste;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Testklasse für die Listen.
*/
public class Pr2LinkedListTest {
@Test
public void testLength() {
Pr2List<String> list = new Pr2LinkedList<>();
assertEquals(0, list.length());
list.add("A");
assertEquals(1, list.length());
list.add("B");
assertEquals(2, list.length());
list.removeLast();
assertEquals(1, list.length());
list.removeLast();
assertEquals(0, list.length());
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
list::removeLast);
assertEquals(0, list.length());
list.add("A");
assertEquals(1, list.length());
list.add("B");
}
@Test
public void testAddRemove() {
Pr2List<String> list = new Pr2LinkedList<>();
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
list::removeLast);
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.remove(0));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.remove(1));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.remove(-1));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.remove(99));
list.add("A");
assertEquals("A", list.removeLast());
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
list::removeLast);
list.add("A");
list.add("B");
list.add("C");
assertEquals("A", list.removeFirst());
assertEquals("C", list.removeLast());
assertEquals("B", list.removeLast());
assertEquals(0, list.length());
list.add("A");
list.add("B");
list.add("C");
assertEquals("A", list.remove(0));
assertEquals("B", list.removeFirst());
assertEquals("C", list.remove(0));
assertEquals(0, list.length());
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.remove(0));
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
System.out.println(list);
assertEquals("C", list.remove(2));
list.add("F");
assertEquals("D", list.remove(2));
assertEquals("F", list.get(3));
assertEquals("E", list.remove(2));
assertEquals("A", list.remove(0));
assertEquals("B", list.remove(0));
assertEquals("F", list.remove(0));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.remove(0));
list.add("A");
assertEquals("A", list.remove(0));
}
@Test
public void testGet() {
Pr2List<String> list = new Pr2LinkedList<>();
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.get(0));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.get(-9));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.get(999));
list.add("A");
assertEquals("A", list.get(0));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.get(1));
list.add("B");
assertEquals("A", list.get(0));
assertEquals("B", list.get(1));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> list.get(2));
list.free();
assertEquals(0, list.length());
list.add("A");
list.add("B");
list.add("C");
assertEquals("C", list.get(2));
assertEquals("A", list.get(0));
assertEquals("B", list.get(1));
}
}

View File

@ -0,0 +1,61 @@
package pr2.datenstrukturen.liste;
/**
* Interface für eine Liste.
*
* @param <T> Tyo der gespeicherten Daten.
*/
public interface Pr2List<T> {
/**
* Fügt das Element e an das Ende der Liste an.
*
* @param e das anzufügende Element
*/
void add(T e);
/**
* Liefert das Element an der gegebenen Position zurück.
*
* @param index Index des Elements.
* @return das gefundene Element.
* @throws IndexOutOfBoundsException wenn der Index zu groß
* oder zu klein ist
*/
T get(int index);
/**
* Entfernt das Element an der gegebenen Stelle.
*
* @param index der Index, des Elements.
* @return Das entfernte Element.
* @throws IndexOutOfBoundsException wenn der Index zu groß
* oder zu klein ist
*/
T remove(int index);
/**
* Gibt die Länge der Liste zurück.
* @return Länge der Liste.
*/
int length();
/**
* Entfernt das letzte Element aus der Liste.
* @return Das entfernte Element.
* @throws IndexOutOfBoundsException wenn die Liste leer ist
*/
T removeLast();
/**
* Entfernt das erste Element aus der Liste.
* @return Das entfernte Element.
* @throws IndexOutOfBoundsException wenn die Liste leer ist
*/
T removeFirst();
/**
* Leert die komplette Liste.
*/
void free();
}

View File

@ -0,0 +1,23 @@
package pr2.datenstrukturen.liste;
/**
* Ein Knoten in der Liste.
*
* @param <T> Tyo der gespeicherten Daten.
*/
public class Pr2Node<T> {
/**
* Das gespeicherte Element.
*/
T element;
/**
* Der Nachfolger.
*/
Pr2Node<T> next;
public Pr2Node(T e) {
element = e;
}
}

View File

@ -0,0 +1,31 @@
package pr2.datenstrukturen.liste;
public class TestLists {
public static void main(String[] args) {
IntList ll = new LinkedList();
ll.append(1);
ll.append(2);
ll.append(3);
ll.append(4);
ll.append(5);
ll.prepend(0);
System.out.println(ll);
System.out.println(ll.size());
IntList dll = new DoubleLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
dll.prepend(0);
System.out.println(dll);
System.out.println(dll.size());
}
}

View File

@ -0,0 +1,42 @@
package pr2.datenstrukturen.queue;
import java.util.LinkedList;
public class Queue<T> {
private LinkedList<T> elements;
public Queue() {
elements = new LinkedList<T>();
}
// Element in die Queue legen
public void enqueue(T element) {
elements.addLast(element);
}
// erstes Element aus der Queue entfernen und zurückgeben
public T dequeue() {
if (elements.isEmpty()) {
return null;
}
return elements.removeFirst();
}
// erstes Element zurückgeben, ohne es zu entfernen
public T peek() {
if (elements.isEmpty()) {
return null;
}
return elements.getFirst();
}
// Queue leer?
public boolean isEmpty() {
return elements.isEmpty();
}
// Anzahl der Elemente in der Queue
public int size() {
return elements.size();
}
}

View File

@ -0,0 +1,52 @@
package pr2.datenstrukturen.search;
public class BoyerMoore {
private final int R; // the radix
private int[] right; // the bad-character skip array
private char[] pattern; // store the pattern as a character array
private String pat; // or as a string
public BoyerMoore(String pat) {
this.R = 256;
this.pat = pat;
// position of rightmost occurrence of c in the pattern
right = new int[R];
for (int c = 0; c < R; c++)
right[c] = -1;
for (int j = 0; j < pat.length(); j++)
right[pat.charAt(j)] = j;
}
public int search(String txt) {
int M = pat.length();
int N = txt.length();
int skip;
for (int i = 0; i <= N - M; i += skip) {
skip = 0;
for (int j = M-1; j >= 0; j--) {
if (pat.charAt(j) != txt.charAt(i+j)) {
skip = Math.max(1, j - right[txt.charAt(i+j)]);
break;
}
}
if (skip == 0) return i; // found
}
return N; // not found
}
public static void main(String[] args) {
String txt = "This is a simple example text";
String pat = "example";
BoyerMoore bm = new BoyerMoore(pat);
int offset = bm.search(txt);
// print results
System.out.println("text: " + txt);
System.out.print("pattern: ");
for (int i = 0; i < offset; i++)
System.out.print(" ");
System.out.println(pat);
}
}

View File

@ -0,0 +1,38 @@
package pr2.datenstrukturen.search;
public class KMP {
private final int[] prefixFunction;
private final String pattern;
public KMP(String pattern) {
this.pattern = pattern;
prefixFunction = new int[pattern.length()];
int j = 0;
for (int i = 1; i < pattern.length(); i++) {
while (j > 0 && pattern.charAt(i) != pattern.charAt(j)) {
j = prefixFunction[j - 1];
}
if (pattern.charAt(i) == pattern.charAt(j)) {
j++;
}
prefixFunction[i] = j;
}
}
public int search(String text) {
int j = 0;
for (int i = 0; i < text.length(); i++) {
while (j > 0 && text.charAt(i) != pattern.charAt(j)) {
j = prefixFunction[j - 1];
}
if (text.charAt(i) == pattern.charAt(j)) {
j++;
}
if (j == pattern.length()) {
return i - pattern.length() + 1;
}
}
return -1;
}
}

View File

@ -0,0 +1,42 @@
package pr2.datenstrukturen.stack;
import java.util.ArrayList;
public class Stack<T> {
private final ArrayList<T> elements;
public Stack() {
elements = new ArrayList<T>();
}
// Element auf den Stack legen
public void push(T element) {
elements.add(element);
}
// oberstes Element vom Stack entfernen und zurückgeben
public T pop() {
if (elements.isEmpty()) {
return null;
}
return elements.remove(elements.size() - 1);
}
// oberstes Element zurückgeben, ohne es zu entfernen
public T peek() {
if (elements.isEmpty()) {
return null;
}
return elements.get(elements.size() - 1);
}
// Stack leer?
public boolean isEmpty() {
return elements.isEmpty();
}
// Anzahl der Elemente im Stack
public int size() {
return elements.size();
}
}

Some files were not shown because too many files have changed in this diff Show More