• 队列 ADT


      队列(queue)是插入在表的末端,删除是在表的开头。对队列的基本操作是 enqueue(入队)和 dequeue(出队),入队是在表的末端(队尾)插入一个元素,出队是在表的开头(队头)删除一个元素

      队列模型说明:通过 enqueue 向队列输入,通过 dequeue 向队列输出

      

      •  队列的数组实现
        public class ArrayQueue<T> {
        
            //设置为2是为了检验ensureCapacity()方法的正确性
            private Object[] data = new Object[2];
        
            private int size;
          //是否空队列
            public boolean empty() {
                return size == 0;
            }
        
            //入队
            public void enqueue(T t) {
                ensureCapacity();
                data[size++] = t;
            }
        
            //出队
            public T dequeue() {
                T t = (T) data[0];
                for (int i = 0; i < size - 1; i++) {
                    data[i] = data[i + 1];
                }
                size--;
                return t;
            }
        
            //确保容量
            private void ensureCapacity() {
                if (size < data.length) {
                    return;
                }
                data = Arrays.copyOf(data, data.length + (data.length >> 1));
            }
        
            public static void main(String[] args) {
                ArrayQueue<String> queue = new ArrayQueue<>();
                char c[] = "Array Queue".toCharArray();
                for (int i = 0; i < c.length; i++) {
                    queue.enqueue(String.valueOf(c[i]));
                }
                while (!queue.empty()) {
                    System.out.print(queue.dequeue());
                }
            }
        }
    • 队列的链表实现
  • 相关阅读:
    Codeforces Round #336 B
    Codeforces Round #336 A
    hiho一下157
    Codeforces Round #420 E
    Codeforces Round #420 C
    Codeforces Round #420 B
    Codeforces Round #420 A
    Codeforces Round #418 C
    Codeforces Round #418 B
    CodeForces 811D Vladik and Favorite Game bfs,模拟
  • 原文地址:https://www.cnblogs.com/cmdra/p/5940167.html
Copyright © 2020-2023  润新知