Compare commits
3 Commits
a661f40a7c
...
28e9ad35b6
Author | SHA1 | Date | |
---|---|---|---|
|
28e9ad35b6 | ||
|
12b7b279c0 | ||
|
f829b973eb |
@ -1,11 +1,15 @@
|
||||
package part3.aufg1;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* a) Realisieren einen Integer‐Stapel. Dabei soll die Klasse Stack aber nicht verwendet
|
||||
* werden, sondern eine dynamische Struktur wie in der Vorlesung „Algorithmen und
|
||||
* Datenstrukturen“ vorgestellt selbst realisiert werden.
|
||||
* Schreiben Sie eine Klasse IntegerStack, die den Stapel mit den Methoden public
|
||||
* void push(int i)und public int pop()zur Verfügung stellt.
|
||||
* Schreiben Sie eine Klasse IntegerStack, die den Stapel mit den Methoden
|
||||
* public void push(int i) und
|
||||
* public int pop()
|
||||
* zur Verfügung stellt.
|
||||
* Schreiben Sie dann eine Klasse Anwendung, in der die Funktionalität Ihres Integer‐
|
||||
* Stapels anhand der im Skript vorgestellten Reihenfolge von Operationen demonstriert
|
||||
* wird.
|
||||
@ -18,7 +22,65 @@ package part3.aufg1;
|
||||
*/
|
||||
|
||||
public class Anwendung {
|
||||
public static void main(String[] args){
|
||||
public void myIntStackDemo(){
|
||||
IntStack myIntStack = new IntStack();
|
||||
|
||||
myIntStack.print();
|
||||
myIntStack.p_push(7);
|
||||
myIntStack.p_push(3);
|
||||
myIntStack.p_push(8);
|
||||
myIntStack.p_push(2);
|
||||
|
||||
myIntStack.p_pop();
|
||||
myIntStack.p_pop();
|
||||
|
||||
myIntStack.p_push(7);
|
||||
|
||||
myIntStack.p_pop();
|
||||
myIntStack.p_pop();
|
||||
myIntStack.p_pop();
|
||||
}
|
||||
public void myGenericStackDemo(){
|
||||
GenericStack myStack = new GenericStack();
|
||||
|
||||
myStack.print();
|
||||
myStack.p_push(1);
|
||||
myStack.p_push('B');
|
||||
myStack.p_push(true);
|
||||
|
||||
myStack.p_pop();
|
||||
myStack.p_push("\"Ich Liebe Generische Typen\"");
|
||||
}
|
||||
|
||||
public void notMyStack_Demo(){
|
||||
Stack javaStack = new Stack();
|
||||
|
||||
System.out.println(javaStack);
|
||||
javaStack.push(1);
|
||||
System.out.println(javaStack);
|
||||
javaStack.push('B');
|
||||
System.out.println(javaStack);
|
||||
javaStack.pop();
|
||||
System.out.println(javaStack);
|
||||
javaStack.push("\"Java kann Stacks!\"");
|
||||
System.out.println(javaStack);
|
||||
javaStack.pop();
|
||||
System.out.println(javaStack);
|
||||
javaStack.pop();
|
||||
System.out.println(javaStack);
|
||||
javaStack.push(true);
|
||||
System.out.println(javaStack);
|
||||
}
|
||||
public static void main(String[] args){
|
||||
Anwendung demo = new Anwendung();
|
||||
demo.myIntStackDemo();
|
||||
|
||||
System.out.println("=====/////======");
|
||||
|
||||
demo.myGenericStackDemo();
|
||||
|
||||
System.out.println("=====/////======");
|
||||
|
||||
demo.notMyStack_Demo();
|
||||
}
|
||||
}
|
||||
|
51
src/part3/aufg1/GenericStack.java
Normal file
51
src/part3/aufg1/GenericStack.java
Normal file
@ -0,0 +1,51 @@
|
||||
package part3.aufg1;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class GenericStack<T> {
|
||||
Object[] myArray;
|
||||
private int MAX_SIZE;
|
||||
private int firstFreeIndex;
|
||||
|
||||
public GenericStack(){
|
||||
this.MAX_SIZE = 100;
|
||||
this.firstFreeIndex = 0;
|
||||
this.myArray = new Object[MAX_SIZE];
|
||||
}
|
||||
|
||||
public void push(T t){
|
||||
this.myArray[this.firstFreeIndex] = t;
|
||||
this.firstFreeIndex++;
|
||||
}
|
||||
|
||||
public void p_push(T t){
|
||||
this.push(t);
|
||||
this.print();
|
||||
}
|
||||
|
||||
public T pop(){
|
||||
return (T) this.myArray[--this.firstFreeIndex];
|
||||
}
|
||||
|
||||
public T p_pop(){
|
||||
T temp = this.pop();
|
||||
this.print();
|
||||
return temp;
|
||||
}
|
||||
|
||||
public Object[] toArray(){
|
||||
if (this.firstFreeIndex<1){
|
||||
return new Object[0];
|
||||
}
|
||||
Object[] retArray = new Object[this.firstFreeIndex];
|
||||
for (int i=0; i<firstFreeIndex; i++){
|
||||
retArray[i] = this.myArray[i];
|
||||
}
|
||||
return retArray;
|
||||
}
|
||||
|
||||
public void print(){
|
||||
System.out.println(Arrays.toString(this.toArray()));
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,50 @@
|
||||
package part3.aufg1;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class IntStack {
|
||||
private int MAX_SIZE;
|
||||
private int[] myArray;
|
||||
private int firstFreeIndex;
|
||||
|
||||
public IntStack(){
|
||||
this.MAX_SIZE = 100;
|
||||
this.firstFreeIndex = 0;
|
||||
this.myArray = new int[MAX_SIZE];
|
||||
}
|
||||
|
||||
public void push(int value){
|
||||
this.myArray[this.firstFreeIndex] = value;
|
||||
this.firstFreeIndex++;
|
||||
}
|
||||
|
||||
public void p_push(int value){
|
||||
this.push(value);
|
||||
this.print();
|
||||
}
|
||||
|
||||
public int pop(){
|
||||
return this.myArray[--this.firstFreeIndex];
|
||||
}
|
||||
|
||||
public int p_pop(){
|
||||
int temp = this.pop();
|
||||
this.print();
|
||||
return temp;
|
||||
}
|
||||
|
||||
public int[] toArray(){
|
||||
if (firstFreeIndex<1){
|
||||
return new int[0];
|
||||
}
|
||||
int[] retArray = new int[firstFreeIndex];
|
||||
for (int i=0; i<firstFreeIndex; i++){
|
||||
retArray[i] = this.myArray[i];
|
||||
}
|
||||
return retArray;
|
||||
}
|
||||
|
||||
public void print(){
|
||||
System.out.println(Arrays.toString(this.toArray()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user