Stack/ActionHistory.java
Sobottasgithub 509adad1d6 Initial
2026-01-09 23:15:03 +01:00

61 lines
1.2 KiB
Java

public class ActionHistory {
private class Node<T> {
private T content = null;
private Node nextNode = null;
public Node(T content) {
setContent(content);
}
private void setContent(T content) {
this.content = content;
}
private T getContent() {
return this.content;
}
private void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
private Node getNextNode() {
return this.nextNode;
}
}
private Node head = null;
public ActionHistory() {}
public boolean isEmpty() {
if (head == null) {
return true;
}
return false;
}
public void push(Object content) {
if (isEmpty()) {
head = new Node(content);
} else {
Node newNode = new Node(content);
newNode.setNextNode(head);
head = newNode;
}
}
public Object pop() {
if (isEmpty()) {
return null;
}
Object content = head.getContent();
head = head.getNextNode();
return content;
}
public Object top() {
return ((isEmpty()) ? null : head.getContent());
}
}