zoukankan      html  css  js  c++  java
  • 数组队列 ArrayQueue

    package datastructures.queue;
    
    /**
     * @author warriorg
     */
    public class ArrayQueue<T> implements Queue<T> {
        private final Object[] items;
        private int takeIndex;
        private int putIndex;
        private int count;
    
    
        public ArrayQueue(int capacity) {
            this.items = new Object[capacity];
            this.takeIndex = 0;
            this.putIndex = 0;
        }
    
        private void enqueue(T item) {
            items[putIndex] = item;
            if (++putIndex == items.length) {
                putIndex = 0;
            }
            count++;
        }
    
        private T dequeue() {
            T t = (T) items[takeIndex];
            items[takeIndex] = null;
            if (++takeIndex == items.length) {
                takeIndex = 0;
            }
            count--;
            return t;
        }
    
        @Override
        public boolean offer(T elem) {
            if (isFull()) {
                return false;
            } 
    
            enqueue(elem);
            return true;
        }
    
        @Override
        public T poll() {
            if (isEmpty()) {
                return null;
            }
            return dequeue();
        }
    
        @Override
        public T peek() {
           return (T)items[takeIndex];
        }
    
        @Override
        public boolean isEmpty() {
            return count == 0;
        }
    
        @Override
        public int size() {
            return count;
        }
    
        public boolean isFull() {
            return count == items.length;
        }
    
        public static void main(String[] args) {
            ArrayQueue<Integer> queue = new ArrayQueue<>(4);
            queue.offer(1);
            queue.offer(2);
            queue.offer(3);
            queue.offer(4);     
            System.out.println("size: " + queue.size());
            System.out.println(queue.poll());
            System.out.println(queue.poll());
            System.out.println(queue.poll());
            System.out.println(queue.poll());
        }
    }
    
  • 相关阅读:
    (中等) HDU 1495 非常可乐,BFS。
    (简单) POJ 1562 Oil Deposits,BFS。
    (简单) POJ 3984 迷宫问题,BFS。
    动态规划(斐波那契系列)---爬楼梯
    回溯---N皇后
    回溯---数独
    回溯---分割字符串使得每个部分都是回文数
    回溯---含有相同元素求子集
    回溯---子集
    回溯---组合求和
  • 原文地址:https://www.cnblogs.com/warrior/p/13353383.html
Copyright © 2011-2022 走看看