Compare commits
4 Commits
12997ba989
...
353a41ba0d
Author | SHA1 | Date | |
---|---|---|---|
353a41ba0d | |||
e732fc7232 | |||
8a7d98881e | |||
f6a3b314cb |
74
VL13/Aufgabe01/Aufgabe01.java
Normal file
74
VL13/Aufgabe01/Aufgabe01.java
Normal file
@ -0,0 +1,74 @@
|
||||
package VL13.Aufgabe01;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Aufgabe01 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Integer Stack
|
||||
System.out.printf("\n%s:\n", "Integer Stack");
|
||||
|
||||
IntegerStack integerStack = new IntegerStack();
|
||||
integerStack.push(7);
|
||||
integerStack.push(3);
|
||||
integerStack.push(8);
|
||||
integerStack.push(2);
|
||||
System.out.println(integerStack.pop());
|
||||
System.out.println(integerStack.pop());
|
||||
integerStack.push(7);
|
||||
System.out.println(integerStack.pop());
|
||||
System.out.println(integerStack.pop());
|
||||
|
||||
// Generic Stack (Integer)
|
||||
System.out.printf("\n%s:\n", "Generic Stack (Integer)");
|
||||
|
||||
GenericStack<Integer> gIntegerStack = new GenericStack<Integer>();
|
||||
gIntegerStack.push(7);
|
||||
gIntegerStack.push(3);
|
||||
gIntegerStack.push(8);
|
||||
gIntegerStack.push(2);
|
||||
System.out.println(gIntegerStack.pop());
|
||||
System.out.println(gIntegerStack.pop());
|
||||
gIntegerStack.push(7);
|
||||
System.out.println(gIntegerStack.pop());
|
||||
System.out.println(gIntegerStack.pop());
|
||||
|
||||
// Generic Stack (String)
|
||||
System.out.printf("\n%s:\n", "Generic Stack (String)");
|
||||
|
||||
GenericStack<String> gStringStack = new GenericStack<String>();
|
||||
gStringStack.push("Hallo");
|
||||
gStringStack.push("Welt");
|
||||
gStringStack.push("!");
|
||||
|
||||
System.out.println(gStringStack.pop());
|
||||
System.out.println(gStringStack.pop());
|
||||
System.out.println(gStringStack.pop());
|
||||
|
||||
// Stack (Integer)
|
||||
System.out.printf("\n%s:\n", "Stack (Integer)");
|
||||
|
||||
Stack<Integer> nIntegerStack = new Stack<Integer>();
|
||||
nIntegerStack.push(7);
|
||||
nIntegerStack.push(3);
|
||||
nIntegerStack.push(8);
|
||||
nIntegerStack.push(2);
|
||||
System.out.println(nIntegerStack.pop());
|
||||
System.out.println(nIntegerStack.pop());
|
||||
nIntegerStack.push(7);
|
||||
System.out.println(nIntegerStack.pop());
|
||||
System.out.println(nIntegerStack.pop());
|
||||
|
||||
// Stack (String)
|
||||
System.out.printf("\n%s:\n", "Stack (String)");
|
||||
|
||||
Stack<String> stringStack = new Stack<String>();
|
||||
stringStack.push("Hallo");
|
||||
stringStack.push("Welt");
|
||||
stringStack.push("!");
|
||||
|
||||
System.out.println(stringStack.pop());
|
||||
System.out.println(stringStack.pop());
|
||||
System.out.println(stringStack.pop());
|
||||
}
|
||||
}
|
67
VL13/Aufgabe01/GenericStack.java
Normal file
67
VL13/Aufgabe01/GenericStack.java
Normal file
@ -0,0 +1,67 @@
|
||||
package VL13.Aufgabe01;
|
||||
|
||||
/**
|
||||
* A generic stack.
|
||||
*/
|
||||
public class GenericStack<T> {
|
||||
StackItem<T> start;
|
||||
|
||||
/**
|
||||
* A stack item to store a single item on the stack.
|
||||
*/
|
||||
private class StackItem<E> {
|
||||
E value;
|
||||
StackItem<E> next;
|
||||
StackItem<E> prev;
|
||||
|
||||
StackItem(E value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to push a value to the stack.
|
||||
*
|
||||
* @param value The value to store on the stack.
|
||||
*/
|
||||
public void push(T value) {
|
||||
if (start == null) {
|
||||
start = new StackItem<T>(value);
|
||||
} else {
|
||||
StackItem<T> item = start;
|
||||
|
||||
// get the last item of the stack.
|
||||
while (item.next != null) {
|
||||
item = item.next;
|
||||
}
|
||||
|
||||
// create a new stack item to store the value.
|
||||
item.next = new StackItem<T>(value);
|
||||
item.next.prev = item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to pop a value from the stack.
|
||||
*
|
||||
* @return The value from stack.
|
||||
*/
|
||||
public T pop() {
|
||||
StackItem<T> item = start;
|
||||
|
||||
// get the last item of the stack.
|
||||
while (item.next != null) {
|
||||
item = item.next;
|
||||
}
|
||||
|
||||
// remove last item from stack.
|
||||
if (item.prev == null) {
|
||||
start = null;
|
||||
} else {
|
||||
item.prev.next = null;
|
||||
}
|
||||
|
||||
// return the value of the last item of the stack.
|
||||
return item.value;
|
||||
}
|
||||
}
|
64
VL13/Aufgabe01/IntegerStack.java
Normal file
64
VL13/Aufgabe01/IntegerStack.java
Normal file
@ -0,0 +1,64 @@
|
||||
package VL13.Aufgabe01;
|
||||
|
||||
/**
|
||||
* A integer stack.
|
||||
*/
|
||||
public class IntegerStack {
|
||||
StackItem start;
|
||||
|
||||
private class StackItem {
|
||||
int value;
|
||||
StackItem next;
|
||||
StackItem prev;
|
||||
|
||||
StackItem(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to push a value to the stack.
|
||||
*
|
||||
* @param value The value to store on the stack.
|
||||
*/
|
||||
public void push(int value) {
|
||||
if (start == null) {
|
||||
start = new StackItem(value);
|
||||
} else {
|
||||
StackItem item = start;
|
||||
|
||||
// get the last item of the stack.
|
||||
while (item.next != null) {
|
||||
item = item.next;
|
||||
}
|
||||
|
||||
// create a new stack item to store the value.
|
||||
item.next = new StackItem(value);
|
||||
item.next.prev = item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to pop a value from the stack.
|
||||
*
|
||||
* @return The value from stack.
|
||||
*/
|
||||
public int pop() {
|
||||
StackItem item = start;
|
||||
|
||||
// get the last item of the stack.
|
||||
while (item.next != null) {
|
||||
item = item.next;
|
||||
}
|
||||
|
||||
// remove last item from stack.
|
||||
if (item.prev == null && item == start) {
|
||||
start = null;
|
||||
} else {
|
||||
item.prev.next = null;
|
||||
}
|
||||
|
||||
// return the value of the last item of the stack.
|
||||
return item.value;
|
||||
}
|
||||
}
|
23
VL13/Aufgabe02/Aufgabe02.java
Normal file
23
VL13/Aufgabe02/Aufgabe02.java
Normal file
@ -0,0 +1,23 @@
|
||||
package VL13.Aufgabe02;
|
||||
|
||||
public class Aufgabe02 {
|
||||
public static void main(String[] args) {
|
||||
MyQueue<Integer> integerQueue = new MyQueue<Integer>();
|
||||
|
||||
// fill the queue with some values.
|
||||
System.out.printf("\n%s:\n", "Aufbau der Queue");
|
||||
|
||||
for (int i = 0; i < 150; i++) {
|
||||
integerQueue.add(i);
|
||||
System.out.printf("Elemente: %d - Größe: %d\n", integerQueue.size(), integerQueue.internalSize());
|
||||
}
|
||||
|
||||
// clear the queue to check the resizing of the internal storage.
|
||||
System.out.printf("\n%s:\n", "Abbau der Queue");
|
||||
|
||||
while (integerQueue.size() > 0) {
|
||||
integerQueue.get();
|
||||
System.out.printf("Elemente: %d - Größe: %d\n", integerQueue.size(), integerQueue.internalSize());
|
||||
}
|
||||
}
|
||||
}
|
64
VL13/Aufgabe02/MyQueue.java
Normal file
64
VL13/Aufgabe02/MyQueue.java
Normal file
@ -0,0 +1,64 @@
|
||||
package VL13.Aufgabe02;
|
||||
|
||||
public class MyQueue<T> {
|
||||
private Object[] myArray;
|
||||
private int numberOfElements;
|
||||
private int maxSize;
|
||||
|
||||
public MyQueue() {
|
||||
this.numberOfElements = 0;
|
||||
this.maxSize = 100;
|
||||
myArray = new Object[this.maxSize];
|
||||
}
|
||||
|
||||
public void add(T t) {
|
||||
this.myArray[this.numberOfElements] = t;
|
||||
this.numberOfElements++;
|
||||
this.adjustInternalArray();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get() {
|
||||
T retValue = (T) this.myArray[0];
|
||||
|
||||
for (int i = 0; i < this.numberOfElements; i++) {
|
||||
this.myArray[i] = this.myArray[i + 1];
|
||||
}
|
||||
|
||||
this.numberOfElements--;
|
||||
this.adjustInternalArray();
|
||||
return retValue;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.numberOfElements;
|
||||
}
|
||||
|
||||
public int internalSize() {
|
||||
return this.maxSize;
|
||||
}
|
||||
|
||||
private void adjustInternalArray() {
|
||||
final int MIN_SIZE = 20;
|
||||
|
||||
while (this.numberOfElements <= Math.ceil(this.maxSize * 0.2) && this.maxSize > MIN_SIZE) {
|
||||
this.maxSize = Math.max(this.maxSize / 2, 20);
|
||||
Object[] myOldArray = this.myArray;
|
||||
this.myArray = new Object[this.maxSize];
|
||||
|
||||
for (int i = 0; i < this.myArray.length; i++) {
|
||||
this.myArray[i] = myOldArray[i];
|
||||
}
|
||||
}
|
||||
|
||||
while (this.numberOfElements >= Math.ceil(this.maxSize * 0.8)) {
|
||||
this.maxSize = this.maxSize * 2;
|
||||
Object[] myOldArray = this.myArray;
|
||||
this.myArray = new Object[this.maxSize];
|
||||
|
||||
for (int i = 0; i < myOldArray.length; i++) {
|
||||
this.myArray[i] = myOldArray[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
135
VL13/Aufgabe03/Aufgabe03.java
Normal file
135
VL13/Aufgabe03/Aufgabe03.java
Normal file
@ -0,0 +1,135 @@
|
||||
package VL13.Aufgabe03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Vorlesung 13 / Aufgabe 3
|
||||
*
|
||||
* @author Sebastian Brosch
|
||||
*/
|
||||
public class Aufgabe03 {
|
||||
public static void main(String[] args) {
|
||||
final int MAX_RUNS = 10;
|
||||
int[] sizes = { 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000,
|
||||
50000000 };
|
||||
|
||||
for (int size : sizes) {
|
||||
long sumArrayList = 0;
|
||||
long sumLinkedList = 0;
|
||||
|
||||
for (int i = 0; i < MAX_RUNS; i++) {
|
||||
ArrayList<String> values = getValues(size);
|
||||
sumArrayList += runTestArrayList(values);
|
||||
sumLinkedList += runTestLinkedList(values);
|
||||
}
|
||||
|
||||
System.out.printf("\nMittelwert (Zeichenketten: %,d):\n", size);
|
||||
System.out.printf(" ArrayList: %d Millisekunden\n", (sumArrayList / MAX_RUNS));
|
||||
System.out.printf(" LinkedList: %d Millisekunden\n", (sumLinkedList / MAX_RUNS));
|
||||
}
|
||||
|
||||
System.out.println("\nAlle Tests wurden beendet.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to run a test using a ArrayList.
|
||||
*
|
||||
* @param values The values to fill in ArrayList.
|
||||
* @return The elapsed time of the test.
|
||||
*/
|
||||
private static long runTestArrayList(ArrayList<String> values) {
|
||||
long timeStart = System.currentTimeMillis();
|
||||
ArrayList<String> testArrayList = new ArrayList<String>();
|
||||
|
||||
for (String value : values) {
|
||||
testArrayList.add(value);
|
||||
}
|
||||
|
||||
return System.currentTimeMillis() - timeStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to run a test using LinkedList.
|
||||
*
|
||||
* @param values The values to fill in LinkedList.
|
||||
* @return The elapsed time of the test.
|
||||
*/
|
||||
private static long runTestLinkedList(ArrayList<String> values) {
|
||||
long timeStart = System.currentTimeMillis();
|
||||
LinkedList<String> testLinkedList = new LinkedList<String>();
|
||||
|
||||
for (String value : values) {
|
||||
testLinkedList.add(value);
|
||||
}
|
||||
|
||||
return System.currentTimeMillis() - timeStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to determine a random number from a certain range.
|
||||
*
|
||||
* @param start The first number of the range.
|
||||
* @param end The last number of the range.
|
||||
* @return A random number from a certain range.
|
||||
*/
|
||||
private static int getRandomNumber(int start, int end) {
|
||||
return (new Random()).nextInt((end - start + 1)) + start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate a random string.
|
||||
*
|
||||
* @param min The minimum length of the random string.
|
||||
* @param max The maximum length of the random string.
|
||||
* @return A random string.
|
||||
*/
|
||||
private static String getRandomString(int min, int max) {
|
||||
final String NUMBERS = "0123456789";
|
||||
final String UCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
final String LCHARS = UCHARS.toLowerCase();
|
||||
|
||||
String random = "";
|
||||
int length = getRandomNumber(min, max);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
String chars = "";
|
||||
|
||||
switch (getRandomNumber(1, 3)) {
|
||||
case 1:
|
||||
chars = NUMBERS;
|
||||
break;
|
||||
case 2:
|
||||
chars = UCHARS;
|
||||
break;
|
||||
case 3:
|
||||
chars = LCHARS;
|
||||
break;
|
||||
}
|
||||
|
||||
int charIndex = getRandomNumber(0, chars.length() - 1);
|
||||
random += chars.substring(charIndex, charIndex + 1);
|
||||
}
|
||||
|
||||
return random;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a list with random strings.
|
||||
*
|
||||
* @param numberOfStrings The amount of random strings to generate.
|
||||
* @return A list with random strings.
|
||||
*/
|
||||
private static ArrayList<String> getValues(int numberOfStrings) {
|
||||
final int MIN_LENGTH = 10;
|
||||
final int MAX_LENGTH = 16;
|
||||
ArrayList<String> values = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < numberOfStrings; i++) {
|
||||
values.add(getRandomString(MIN_LENGTH, MAX_LENGTH));
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user