package com.datacrafts.digitalevers.queue
object oneWayQueueDemo{
def main(args: Array[String]): Unit = {
}
}
/**
*
*/
class oneWayQueue(maxSize:Int) {
var front = -1
var tail = -1
val length = maxSize
if(maxSize <= 0){
throw new Exception("参数错误")
}
var oneWayQueue = Array[Int](maxSize)
/**
* 队列是否为空
*/
def isEmpty(): Boolean = {
if(front == tail) true else false
}
/**
* 队列是否满员
*/
def isFull():Boolean = {
if(tail >= length - 1) true else false
}
/**
* 队列弹出数据
*/
def getData() = {
if(isEmpty()){
throw new Exception("队列为空")
}
front += 1
oneWayQueue(front)
}
/**
* 队列添加数据
* TODO 亦可以直接返回 Exception 类型使其参与逻辑运算
*/
def addData(inData:Int) = {
if(isFull()){
//throw new Exception("队列已满")
false
} else {
tail += 1
oneWayQueue(tail) = inData
true
}
}
////////
}
近期评论