public class StackImplLinkedList<E> {
private LNode<E> head = new LNode<E>(null, null);
private int maxSize = 0;
private int size = 0;
public StackImplLinkedList(int maxSize) {
this.maxSize = maxSize;
}
public int size() {
return size;
}
public boolean isFull() {
return size == maxSize;
}
public boolean isEmpty() {
return size == 0;
}
public boolean push(E value) {
if(!isFull()) {
LNode<E> current = new LNode<E>(value, head.next);
head.next = current;
size ++;
return true;
}
return false;
}
public E pop() {
if(!isEmpty()) {
LNode<E> current = head.next;
head.next = head.next.next;
size --;
return current.value;
}
return null;
}
public static void main(String[] args) {
StackImplLinkedList<Integer> stack = new StackImplLinkedList<>(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}