队列的数组实现_JAVA描述《数据结构与算法分析》

简介:
None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif public  class QueueArray  {
InBlock.gif    static final int DEFAULT_CAPACITY = 10;
InBlock.gif
InBlock.gif    private Object[] theArray;
InBlock.gif
InBlock.gif    private int currentSize;
InBlock.gif
InBlock.gif    private int front;
InBlock.gif
InBlock.gif    private int back;
InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Construct the queue.
InBlock.gif     * 
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public QueueArray() {
InBlock.gif        this(DEFAULT_CAPACITY);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Construct the queue.
InBlock.gif     * 
InBlock.gif     * 
@param capacity
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public QueueArray(int capacity) {
InBlock.gif        theArray = new Object[capacity];
InBlock.gif        MakeEmpty();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Make the queue logically empty.
InBlock.gif     * 
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public void MakeEmpty() {
InBlock.gif        currentSize = 0;
InBlock.gif        front = 0;
InBlock.gif        back = -1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public boolean IsEmpty() {
InBlock.gif        return currentSize == 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public boolean IsFull() {
InBlock.gif        return currentSize == theArray.length;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Get the least recently inserted item in the queue. Does not alter the
InBlock.gif     * queue.
InBlock.gif     * 
InBlock.gif     * 
@return the least recently inserted item in the queue, or null, if empty.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public Object getFront() {
InBlock.gif        if (IsEmpty())
InBlock.gif            return null;
InBlock.gif        return theArray[front];
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Insert a new item into the queue.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            the item to insert.
InBlock.gif     * 
@throws Overflow
InBlock.gif     *             if queue is full.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public void Enqueue(Object x) throws Overflow {
InBlock.gif        if (IsFull())
InBlock.gif            throw new Overflow();
InBlock.gif        back = increment(back);
InBlock.gif        theArray[back] = x;
InBlock.gif        currentSize++;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Internal method to increment with wraparound.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            any index in theArray`s range.
InBlock.gif     * 
@return x+1,or 0,if x is at the end of theArray;
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    private int increment(int x) {
InBlock.gif        if (++x == theArray.length)
InBlock.gif            x = 0;
InBlock.gif        return x;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Return and remove the least recently inserted item from the queue.
InBlock.gif     * 
InBlock.gif     * 
@return the least recently inserted item, or null, if empty.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public Object Dequeue() {
InBlock.gif        if (IsEmpty())
InBlock.gif            return null;
InBlock.gif        currentSize--;
InBlock.gif
InBlock.gif        Object frontItem = theArray[front];
InBlock.gif        theArray[front] = null;
InBlock.gif        front = increment(front);
InBlock.gif        return frontItem;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/09/11/500814.html ,如需转载请自行联系原作者
相关文章
|
12天前
|
算法 安全 Java
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
【4月更文挑战第28天】性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
28 1
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
|
3天前
栈与队列理解
栈与队列理解
9 1
|
3天前
|
存储 算法
数据结构与算法 栈与队列
数据结构与算法 栈与队列
10 0
数据结构与算法 栈与队列
|
3天前
|
存储 算法 Java
数据结构与算法 数组和链表
数据结构与算法 数组和链表
8 0
|
3天前
|
C++
数据结构(顺序队列 循环队列
数据结构(顺序队列 循环队列
8 0
|
4天前
|
容器
【栈与队列】栈与队列的相互转换OJ题
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
10 0
|
6天前
|
C语言
猿创征文|栈和队列OJ刷题
猿创征文|栈和队列OJ刷题
[数据结构]~栈和队列(0-1)
[数据结构]~栈和队列(0-1)
|
6天前
|
存储 算法
Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
给定一个无序整数数组和目标值,找出数组中和为目标值的两个数的下标。要求不重复且可按任意顺序返回。示例:输入nums = [2,7,11,15], target = 9,输出[0,1]。暴力解法时间复杂度O(n²),优化解法利用哈希表实现,时间复杂度O(n)。
18 0
|
12天前
|
存储
栈与队列练习题
栈与队列练习题