队列的数组实现_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 ,如需转载请自行联系原作者
相关文章
|
1天前
|
算法 测试技术
【数据结构与算法 | 基础篇】环形数组模拟队列
【数据结构与算法 | 基础篇】环形数组模拟队列
|
1天前
|
算法 测试技术
【数据结构与算法 | 基础篇】单向循环链表实现队列
【数据结构与算法 | 基础篇】单向循环链表实现队列
|
1天前
|
存储 算法
【数据结构与算法 | 基础篇】[数组专题]力扣88
【数据结构与算法 | 基础篇】[数组专题]力扣88
|
3天前
|
机器学习/深度学习 存储 算法
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)(下)
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)
4 0
|
3天前
|
算法 前端开发 C语言
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)(上)
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)
11 0
|
3天前
|
存储 算法
数据结构与算法②(复杂度相关OJ)(六道数组OJ题)(下)
数据结构与算法②(复杂度相关OJ)(六道数组OJ题)
6 0
|
3天前
|
存储 算法 C语言
数据结构与算法②(复杂度相关OJ)(六道数组OJ题)(上)
数据结构与算法②(复杂度相关OJ)(六道数组OJ题)
20 2
|
8天前
|
存储 算法
数据结构与算法 栈与队列
数据结构与算法 栈与队列
14 0
数据结构与算法 栈与队列
|
8天前
|
存储 算法 Java
数据结构与算法 数组和链表
数据结构与算法 数组和链表
13 0
|
8天前
|
存储 索引
深入浅出数据结构之数组
深入浅出数据结构之数组