Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DynamicQueue {
- private static class Node {
- private Object element;
- private Node next;
- public Node(Object element, Node next) {
- this.element = element;
- next.next = next;
- }
- Node(Object element) {
- this.element = element;
- this.next = null;
- }
- }
- private Node head;
- private Node tail;
- private int count;
- public DynamicQueue() {
- this.head = null;
- this.tail = null;
- count = 0;
- }
- public void add(Object element) {
- if(head == null) {
- head = new Node(element);
- tail = head;
- } else {
- Node node = new Node(element, tail);
- tail = node;
- }
- count++;
- }
- public Object poll() {
- Object polledElement = head.element;
- count--;
- if(count == 0) {
- tail = null;
- head = null;
- } else {
- int counter = 0;
- DynamicQueue.Node iterator = head;
- while (counter + 1 != count) {
- head = iterator;
- iterator = iterator.next;
- counter++;
- }
- }
- return polledElement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement