2020-09-09 22:37:01 +02:00
|
|
|
|
|
|
|
public class Stack<ContentType> {
|
|
|
|
|
|
|
|
private StackNode<ContentType> head = null;
|
|
|
|
|
|
|
|
public Stack() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void push( ContentType pContentObject ) {
|
2020-09-10 11:04:22 +02:00
|
|
|
if (head != null) {
|
2020-09-10 11:05:59 +02:00
|
|
|
StackNode<ContentType> node = new StackNode<>(pContentObject);
|
2020-09-10 11:04:22 +02:00
|
|
|
node.setNext(head);
|
|
|
|
head = node;
|
|
|
|
} else {
|
2020-09-10 11:05:59 +02:00
|
|
|
head = new StackNode<>(pContentObject);
|
2020-09-10 11:04:22 +02:00
|
|
|
}
|
2020-09-09 22:37:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void pop() {
|
|
|
|
if( head != null ) {
|
|
|
|
head = head.getNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Liefert das Inhaltsobjekt vom obersten Knoten des Stapels
|
|
|
|
* vom Typ ContentType, falls die Schlange nicht leer ist,
|
|
|
|
* ansonsten null.
|
|
|
|
*
|
|
|
|
* @return Das Inhaltsobjekt oder null.
|
|
|
|
*/
|
|
|
|
public ContentType top() {
|
|
|
|
if( !isEmpty() ) {
|
|
|
|
return head.getContent();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prüft, ob der Stapel leer ist.
|
|
|
|
*
|
|
|
|
* @return true, wenn der Stapel keine Knoten enthaelt
|
|
|
|
*/
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return (head == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|