Java Stack mithilfe der verknüpften Liste
class StackUsingLinkedlist {
private class Node {
int data;
Node next;
}
Node top;
StackUsingLinkedlist() {
this.top = null;
}
public void push(int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = top;
top = newNode;
}
public boolean isEmpty() {
return top == null;
}
public int peek() {
if (!isEmpty()) {
return top.data;
} else {
System.out.println("Stack is empty");
return -1;
}
}
public void pop() {
if (top == null) {
System.out.print("\nStack empty");
return;
}
top = top.next;
}
public void display() {
if (top == null) {
System.out.print("\nStack empty");
exit(1);
} else {
Node node = top;
while (node != null) {
System.out.printf("%d->", node.data);
node = node.next;
}
}
}
Magnificent Monkey Adi