forked from IF-LK-2020/queue-stack
51 lines
1.1 KiB
Java
51 lines
1.1 KiB
Java
|
|
public class Stack<ContentType> {
|
|
|
|
private StackNode<ContentType> head = null;
|
|
|
|
public Stack() {
|
|
}
|
|
|
|
|
|
public void push( ContentType pContentObject ) {
|
|
if (head != null) {
|
|
StackNode<ContentType> node = new StackNode<>(pContentObject);
|
|
node.setNext(head);
|
|
head = node;
|
|
} else {
|
|
head = new StackNode<>(pContentObject);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
}
|