博客
关于我
数组、链表实现队列
阅读量:199 次
发布时间:2019-02-28

本文共 1930 字,大约阅读时间需要 6 分钟。

package task2;/** * 功能:数组、链表实现队列 * Created by liumao on 2019/8/7 */public class Queue implements com.imooc.oop.queue.Queue {    /**     * 数组队列实现     */    public static class ArrayQueue {        private Object[] arrQueue;        private int front;        private int rear;        public ArrayQueue(int size) {            arrQueue = new Object[size];            front = 0;            rear = 0;        }        public boolean enQueue(Object obj) {            if ((rear + 1) % arrQueue.length == front) {                return false;            }            arrQueue[rear] = obj;            rear = (rear + 1) % arrQueue.length;            return true;        }        public Object deQueue() {            if (rear == front) {                return null;            }            Object obj = arrQueue[front];            front = (front + 1) % arrQueue.length;            return obj;        }    }    /**     * 链表队列实现     */    public static class LinkedQueue implements com.imooc.oop.queue.Queue {        private Node rear;        private Node front;        private int size;        public LinkedQueue(int size) {            this.size = size;            this.rear = null;            this.front = null;        }        public class Node {            Object t;            Node next;        }        public boolean isEmpty() {            return rear == null;        }        public void enQueue(Object data) {            Node oldLast = front;            front = new Node();            front.next = null;            if (size == 0) {                rear = front;            } else {                oldLast = rear;                rear = front.next;                front.next = rear;            }            size++;        }        public Object deQueue() {            Object t = rear.t;            rear = front.next;            if (size == 0) {                front = null;            }            size--;            return t;        }    }}

转载地址:http://afps.baihongyu.com/

你可能感兴趣的文章
PHP mysql_real_escape_string() 函数防SQL注入
查看>>
php mysql优化方法_MySQL优化常用方法
查看>>
PHP OAuth 2.0 Server
查看>>
php odbc驱动,php常用ODBC函数集(详细)
查看>>
php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
查看>>
php paypal rest api,PayPal REST API指定网络配置文件PHP
查看>>
php pcntl 多进程学习
查看>>
PHP pcntl_fork不能在web服务器中使用的变通方法
查看>>
php private ,public protected三者的区别
查看>>
php PSR规范
查看>>
php rand() 重复,array_rand()函数从另外一个数组中随机取得的一定数量的数组的元素是否会重复?...
查看>>
php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用
查看>>
php redis 集群扩展类文件
查看>>
php redis(2)
查看>>
PHP Redis分布式锁
查看>>
php redis的应用
查看>>
php rss,如何用PHP编写RSS
查看>>
php session超时时间_php怎么设置session超时时间
查看>>
PHP SOAP模块的使用方法:NON-WSDL模式
查看>>
PHP Socket实现websocket(三)Stream函数
查看>>