1. 程式人生 > >java之環形佇列的實現

java之環形佇列的實現

/**
 * Array-based implementation of the queue.
 * @author zhang  yin ye  

 *@email:[email protected]
 */
public class ArrayQueue<AnyType> implements Queue<AnyType>
{
    /**
     * Construct the queue.
     */
    public ArrayQueue( )
    {
        theArray = (AnyType []) new Object[ DEFAULT_CAPACITY ];
        makeEmpty( );
    }

    /**
     * Test if the queue is logically empty.
     * @return true if empty, false otherwise.
     */
    public boolean isEmpty( )
    {
        return currentSize == 0;
    }

    /**
     * Make the queue logically empty.
     */
    public void makeEmpty( )
    {
        currentSize = 0;
        front = 0;
        back = -1;
    }

    /**
     * Return and remove the least recently inserted item
     * from the queue.
     * @return the least recently inserted item in the queue.
     * @throws UnderflowException if the queue is empty.
     */
    public AnyType dequeue( )
    {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayQueue dequeue" );
        currentSize--;

        AnyType returnValue = theArray[ front ];
        front = increment( front );
        return returnValue;
    }
   
    /**
     * Get the least recently inserted item in the queue.
     * Does not alter the queue.
     * @return the least recently inserted item in the queue.
     * @throws UnderflowException if the queue is empty.
     */
    public AnyType getFront( )
    {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayQueue getFront" );
        return theArray[ front ];
    }

    /**
     * Insert a new item into the queue.
     * @param x the item to insert.
     */
    public void enqueue( AnyType x )
    {
        if( currentSize == theArray.length )
            doubleQueue( );
        back = increment( back );
        theArray[ back ] = x;
        currentSize++;
    }

    /**
     * Internal method to increment with wraparound.
     * @param x any index in theArray's range.
     * @return x+1, or 0 if x is at the end of theArray.
     */
    private int increment( int x )
    {
        if( ++x == theArray.length )
            x = 0;
        return x;
    }
   
    /**
     * Internal method to expand theArray.
     */
    private void doubleQueue( )
    {
        AnyType [ ] newArray;

        newArray = (AnyType []) new Object[ theArray.length * 2 ];

            // Copy elements that are logically in the queue
        for( int i = 0; i < currentSize; i++, front = increment( front ) )
            newArray[ i ] = theArray[ front ];

        theArray = newArray;
        front = 0;
        back = currentSize - 1;
    }

    private AnyType [ ] theArray;
    private int         currentSize;
    private int         front;
    private int         back;

    private static final int DEFAULT_CAPACITY = 10;
}

相關推薦

java環形佇列實現

/**  * Array-based implementation of the queue.  * @author zhang  yin ye    *@email:[email protected]  */ public class ArrayQueue&l

資料結構環形佇列實現(1)

注意:判空,判佇列滿, MyQueue.h #ifndef MYQUEUE_H #define MYQUEUE_H /* 環形佇列C++實現 */ class MyQueue{ publ

Java用棧實現隊列

ring idt style enqueue 元素 java 出隊 入隊 span 隊列是一種典型的先進先出數據結構,隊列的實現方式有很多種,比如數組,比如鏈表等,隊列也可以用兩個棧來實現,下面就用兩個棧實現一個隊列。 原理   兩個棧中,一個棧用來入隊,叫他入隊棧,另

javaLock的實現原理

委派 rds use 競爭 從數據 xtend abstract cee 不同 0. 前言 與synchronized不同的是,Lock完全用Java寫成,在java這個層面是無關JVM實現的。 在java.util.concurrent.locks包中有很多Lock的實現

Java執行緒實現原理

 併發不一定依賴多執行緒(如PHP中很常見的多程序併發),但在Java裡談併發,大多數都與執行緒脫不了關係。執行緒是一種比程序更輕量級的排程執行單位,執行緒的引入,可以把一個程序的資源分配和排程執行分開,各個執行緒既可以共享程序資源(記憶體地址、檔案I/O等),又可以獨立排程(執行緒是C

java使用優先順序佇列實現哈夫曼編碼

思路: 構建小根堆 根據小根堆實現哈夫曼樹 根據哈夫曼樹對資料進行編碼 程式碼實現如下: /** * @Author: DaleyZou * @Description: 使用java實現一個哈夫

JAVA氣泡排序—實現雙色球機選小程式

紅球6位:1~33 中不重複的遞增陣列 ,籃球1位:1~16   廢話不多說,上程式碼 pojo package pojo; public class Lottery {public int red;public int blue;public int startRed

java使用阻塞佇列實現生產者消費者模式

Java 5之前實現同步存取時,可以使用普通的一個集合,然後在使用執行緒的協作和執行緒同步可以實現生產者,消費者模式,主要的技術就是用好,wait ,notify,notifyAll,sychronized這些關鍵字。而在java 5之後,可以使用組阻塞佇列來實現,此方式

Javasynchronized的實現原理

0. 前言 目前在Java中存在兩種鎖機制:synchronized和Lock, Lock介面及其實現類是JDK5增加的內容,其作者是大名鼎鼎的併發專家Doug Lea。本文並不比較synchronized與Lock孰優孰劣,只是介紹二者的實現原理。 資料同步需

資料結構佇列實現楊輝三角

/************************************************************** > File Name: PascalTriangle.c > Author: chengfeiyan > Mail:

楊輝三角c++佇列實現

#include<stdlib.h> #include<stdio.h> #include<time.h> #include<iostream> #include<string> #include<vector

windows下多程序通訊,基於共享記憶體環形佇列實現

1 #include "stdafx.h" 2 #include "InterProcessCommunication.h" 3 #include <string> 4 enum 5 { 6 STATE_EMPTY = 0, 7 STATE_READ,

LeetCode141環形連結串列(Java實現)

一、題目   二、兩種解題思路及程式碼實現  ①龜兔賽跑解法,快指標跳兩個,慢指標跳一個,若兩指標遇到一樣,則有環        時間複雜度:O(n)        空間複雜

Java---實現資料結構----佇列

package 資料結構;//java之實現佇列 class quene{ private class Data{ private Object obj; private Data next=null; Data(Object obj){

Java實現環形佇列

這裡我定義的環形佇列為:列表中最後一個元素是指向列表中的第一個元素,而且裡面提供一個next方法,可以不斷獲取下一個元素,在環形佇列中也就是不斷的轉圈,實現方式如下: 佇列中提供的方法: public boolean add(E e):加入佇列

leetcode解題225 # Implement Stack using Queuest Java版 (用兩個佇列實現一個棧)

225. Implement Stack using Queues Implement the following operations of a stack using queues. pu

資料結構Java語言描述迴圈佇列的兩種實現方式

1、佇列的描述 佇列是一種先進先出的儲存資料的結構。如我們現實生活中的排隊就是一個典型的例子。 2、迴圈佇列及其優點   2.1、迴圈佇列是佇列的擴充套件,就是佇列首尾連線,形成一個閉環的圈子。   2.2、優點   充分利用儲存空間。 3、佇列的實現方式  

JAVA資料結構迴圈佇列實現

1、迴圈佇列CircleQueue類的實現程式碼如下所示: public class CircleQueue { private Object[] array; private int capac

Java實現環形佇列(入隊、出隊)

public class Class_queue {     private int q_head;     private int q_tail;     private int[] queue;     private int len;     private int

AQS原始碼深入分析條件佇列-你知道Java中的阻塞佇列是如何實現的嗎?

本文基於JDK-8u261原始碼分析 ------ # 1 簡介 ![img](https://img2020.cnblogs.com/other/1187061/202011/1187061-20201109170509374-960514910.png) 因為CLH佇列中的執行緒,什麼執行緒獲取