class Node1 {
Node1 next;
Node1 prev;
String data;
}
class Stack1 {
Node1 head;
Node1 ptr;
Node1 tmp;
Stack1 () {
head = null;
ptr = head;
}
void push (String data) {
if (head == null) {
head = new Node1();
head.next = null;
head.prev = head;
head.data = data;
ptr = head;
}
else {
tmp = new Node1();
ptr.next = tmp;
tmp.prev = ptr;
tmp.next = null;
tmp.data = data;
ptr = ptr.next;
}
}
String pop () {
String data;
try {
data = ptr.data;
}
catch (java.lang.NullPointerException ex) {
System.out.println ("+++ Empty Stack +++ Empty Stack +++ Empty Stack +++");
}
if (ptr != head) {
data = ptr.data;
ptr = ptr.prev;
ptr.next = null;
return data;
}
else {
data = ptr.data;
ptr = null;
head = null;
return data;
}
}
boolean isEmpty () {
return (head == null);
}
Node1 peek() {
return ptr;
}
}
public class Stackprogram {
public static void main(String [] args) {
Stack1 stck = new Stack1();
stck.push ("David");
stck.push ("Vajda");
stck.push ("Anton");
stck.push ("Berta");
stck.push ("Ceasar");
stck.push ("Dora");
System.out.println (stck.pop());
System.out.println (stck.pop());
System.out.println (stck.pop());
System.out.println (stck.pop());
stck.push ("Emil");
stck.push ("Friedrich");
stck.push ("Gustav");
System.out.println (stck.pop());
System.out.println (stck.pop());
System.out.println (stck.pop());
if (stck.isEmpty())
System.out.println ("Stack is empty");
else
System.out.println ("Stack is not empty");
System.out.println (stck.pop());
System.out.println (stck.pop());
if (stck.isEmpty())
System.out.println ("Stack is empty");
else
System.out.println ("Stack is not empty");
System.out.println (stck.pop());
System.out.println (stck.pop());
}
}