zoukankan      html  css  js  c++  java
  • 《Java数据结构》Java Queue队列

    前言

    学习LinkedList的时候,遇到了队列数据结构,就想着回顾一下队列数据结构。

    原理

    Queue数据结构是一种线性的结构,先进先出的特性。下面看一下逻辑图

    队列在我们写代码的过程比较常用,例如打印日志,一个程序打印日志是比较消耗资源的,一般都会采用队列实现,将需要的日志先放入队列中,由其他线程去

    循环队列输出打印。

    代码

    通过数组实现的定长队列

    public class QueueArray<E> {
    
        // 定义一个数组
        private Object[] data = null;
    
        // 记录队列长度
        private int size = 0;
    
        private int length = 0;
    
        private int point = 0;
    
        /**
         * 默认构造函数
         */
        public QueueArray(){
            length = 10;
            data = new Object[10];
        }
    
        /**
         * 自定义队列长度
         * @param length
         */
        public QueueArray(int length){
            this.length = length;
            data = new Object[length];
        }
    
        /**
         * 往队列里面加入数据
         * @param e
         */
        public void push(E e){
            data[size++] = e;
            size = size%length;
        }
    
        /**
         * 往队列里面获取数据
         */
        public E pull(){
            E e = (E)data[point];
            data[point] = null;
            if(point/length == 1){
                point = 0;
            } else {
                point++;
            }
            return e;
        }
    
        /**
         * 打印队列
         */
        public void println(){
            for(int i =point ;i < size; i++){
                System.out.println(data[i]);
            }
        }
    }
    public class QueueTest {
    
        public static void main(String[] args) {
            QueueArray queueArray = new QueueArray();
            queueArray.push("1");
            queueArray.push(2);
            queueArray.push("5");
            System.out.println(queueArray.pull());
            queueArray.push(7);
            queueArray.push("9");
            System.out.println(queueArray.pull());
            System.out.println(queueArray.pull());
            queueArray.println();
        }
    }

    运行结果:

    通过链表实现的可变长度队列

    public class QueueLinked<E> {
    
        /**
         * 创建内部类链表的节点
         * @param <E>
         */
        private class Node<E>{
            public E data = null; //数据域
            public Node<E> next = null; //指针域
    
            //构造方法
            public Node(){}
    
            public Node(E data){
                this.data = data;
            }
        }
    
        private Node<E> head = null; //队头
        private Node<E> end  = null; //队尾
        private int size = 0;         //队列长度
    
        /**
         * 判断队列是否为空
         * @return
         */
        public boolean isEmpty(){
            return size == 0;
        }
    
        /**
         * 往队列里面加入数据
         * @param e
         */
        public boolean push(E e){
            Node<E> node = new Node<E>(e);
            //队列为空的情况下
            if(isEmpty()){
                this.end = node; //尾节点赋值为新插入的节点
                this.head = this.end; //在只有一个节点的情况下,头尾节点相等
                size++;
                return true;
            }
            //不为空的情况下
            this.end.next = node; //尾节点指向新节点
            this.end = node; //更新尾节点
            size ++; //队列节点数+1
            return true;
        }
    
        /**
         * 出队
         * @return
         */
        public E pop(){
            //判断队列是否为空
            if(isEmpty()){
                System.err.println("队列为空");
                return null;
            }
            //弹出队头,更新队头指针
            Node<E> temp = this.head; //获取队头引用
            this.head = this.head.next; //更新队头指针
            temp.next = null; //释放原队头节点引用
            return temp.data;
        }
    
        /**
         * 打印数据
         */
        public void display(){
            for (int i = 0; i < this.size; i++) {
                if(this.head == null){
                    return;
                }
                System.out.println(this.head.data);
                this.head = this.head.next;
            }
        }
    }
    public class LinkedTest {
        public static void main(String[] args) {
            QueueLinked queueLinked = new QueueLinked();
            queueLinked.push("1");
            queueLinked.push(2);
            queueLinked.push("5");
            System.out.println(queueLinked.pop());
            queueLinked.push(7);
            queueLinked.push("9");
            System.out.println(queueLinked.pop());
            System.out.println(queueLinked.pop());
            queueLinked.display();
        }
    }

    运行结果:

    总结

    队列的数据结构,简单的了解一下,写底层C代码才会使用上,平时用不上可惜。

  • 相关阅读:
    LeetCode(81): 搜索旋转排序数组 II
    2018年6月8日论文阅读
    LeetCode(80):删除排序数组中的重复项 II
    LeetCode(79): 单词搜索
    LeetCode(78):子集
    LeetCode(77):组合
    LeetCode(76): 最小覆盖子串
    LeetCode(75):分类颜色
    LeetCode(74):搜索二维矩阵
    linux 两个查找工具 locate,find
  • 原文地址:https://www.cnblogs.com/jssj/p/13775648.html
Copyright © 2011-2022 走看看