public class QueueImplLinkedList<E> {
private int maxSize = 0;
private int size = 0;
private LNode<E> head = new LNode<>(null, null);
private LNode<E> tail = head;
public QueueImplLinkedList(int maxSize) {
this.maxSize = maxSize;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == maxSize;
}
public int size() {
return size;
}
public boolean add(E value) {
if(!isFull()) {
tail.next = new LNode<E>(value, null);
tail = tail.next;
size ++;
return true;
} else {
return false;
}
}
public E take() {
if(!isEmpty()) {
LNode<E> current = head.next;
head.next = head.next.next;
size --;
return current.value;
}
//the old tail now points to a deleted node
tail = head;
return null;
}
public static void main(String... args) {
QueueImplLinkedList<Integer> queue = new QueueImplLinkedList<>(3);
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());
}
}