Best implementation of Java Queue?

后端 未结 9 674
有刺的猬
有刺的猬 2020-12-23 16:23

I am working (in Java) on a recursive image processing algorithm that recursively traverses the pixels of the image, outwards from a center point.

Unfortunately, tha

相关标签:
9条回答
  • 2020-12-23 16:46

    However, if you still want to use the recursive algorithm, you can change it to be "tail-recursive" which probably is optimized in the JVM to avoid stack overflows.

    0 讨论(0)
  • 2020-12-23 16:48

    I think you can some up with simple like implementation

    package DataStructures;
    
    public class Queue<T> {
    
       private Node<T> root;
    
       public Queue(T value) {
          root = new Node<T>(value);
       }
    
       public void enque(T value) {
          Node<T> node = new Node<T>(value);
          node.setNext(root);
          root = node;
       }
    
       public Node<T> deque() {
    
          Node<T> node = root;
          Node<T> previous = null;
    
          while(node.next() != null) {
             previous = node;
             node = node.next();
          }
          node = previous.next();
          previous.setNext(null);
          return node;
       }
    
       static class Node<T> {
    
          private T value;
          private Node<T> next;
    
          public Node (T value) {
             this.value = value;
          }
    
          public void setValue(T value) {
             this.value = value;
          }
    
          public T getValue() {
             return value;
          }
    
          public void setNext(Node<T> next) {
             this.next = next;
          }
    
          public Node<T> next() {
             return next;
          }
       }
    }
    
    0 讨论(0)
  • 2020-12-23 16:48

    Here is the Queue Implementation with Iterator and Iterable interface

    Queue Size will increase as It gets full

    Queue Interface

    package com.practice.ds.queue;
    
    import com.practice.ds.queue.exception.QueueException;
    
    public interface QueueInterface<T> {
    
        public boolean empty();
    
        public void enqueue(T item);
    
        public void dequeue() throws QueueException;
    
        public T front() throws QueueException;
    
        public void clear();
    }
    

    Custom Exception Class

    package com.practice.ds.queue.exception;
    
    public class QueueException extends Exception {
    
        private static final long serialVersionUID = -884127093599336807L;
    
        public QueueException() {
            super();
        }
    
        public QueueException(String message) {
            super(message);
        }
    
        public QueueException(Throwable e) {
            super(e);
        }
    
        public QueueException(String message, Throwable e) {
            super(message, e);
        }
    }
    

    Implementation of Queue

    package com.practice.ds.queue;
    
    import java.util.Iterator;
    
    import com.practice.ds.queue.exception.QueueException;
    
    public class Queue<T> implements QueueInterface<T>, Iterable<T> {
    
        private static final int DEFAULT_CAPACITY = 10;
        private int current = 0;
        private int rear = 0;
        private T[] queueArray = null;
        private int capacity = 0;
    
        @SuppressWarnings("unchecked")
        public Queue() {
            capacity = DEFAULT_CAPACITY;
            queueArray = (T[]) new Object[DEFAULT_CAPACITY];
            rear = 0;
            current = 0;
        }
    
        @Override
        public boolean empty() {
            return capacity == current;
        }
    
        @Override
        public void enqueue(T item) {
            if(full())
                ensureCapacity();
            queueArray[current] = item;
            current++;
        }
    
        @Override
        public void dequeue() throws QueueException {
            T dequeuedItem = front();
            rear++;
            System.out.println("Dequed Item is " + dequeuedItem);
        }
    
        @Override
        public T front() throws QueueException {
            return queueArray[rear];
        }
    
        @Override
        public void clear() {
            for (int i = 0; i < capacity; i++)
                queueArray[i] = null;
            current = 0;
            rear = 0;
        }
    
        @SuppressWarnings("unchecked")
        private void ensureCapacity() {
            if (rear != 0) {
                copyElements(queueArray);
            } else {
                capacity *= 2;
                T[] tempQueueArray = (T[]) new Object[capacity];
                copyElements(tempQueueArray);
            }
            current -= rear;
            rear = 0;
        }
    
        private void copyElements(T[] array) {
            for (int i = rear; i < current; i++)
                array[i - rear] = queueArray[i];
            queueArray = array;
        }
    
        @Override
        public Iterator<T> iterator() {
            return new QueueItearator<T>();
        }
    
        public boolean full() {
            return current == capacity;
        }
    
        private class QueueItearator<T> implements Iterator<T> {
    
            private int index = rear;
    
            @Override
            public boolean hasNext() {
                return index < current;
            }
    
            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) queueArray[index++];
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题