java数据结构 – 队列代码演示
来源:http://blog.csdn.net/sdliujiangbo/article/details/8069431
public class Queue {
private int size; //当前队列元素个数
private int[] Array;//存放队列元素的数组
private int MaxSize;//队列最大尺寸
//构造函数
public Queue(int maxsize){
MaxSize = maxsize;
Array = new int[MaxSize];
size = 0;
}
//判断队列是否为空
public int IsEmpty(){
if(size == 0)
return 0;
return -1;
}
//判断队列是否为满
public int IsFull(){
if(size == MaxSize)
return 0;
return -1;
}
//返回队列长度
public int GetLength(){
return this.size;
}
//队列插入
public int EnQueue(int x){
//若队列不满,把x插到队尾,返回0;否则返回-1;
if(IsFull() == -1){
Array[size] = x;
size++;
return 0;
}
return -1;
}
//队列删除
public int DeEmpty(){
//若队列不空,则删除对头元素,返回该元素的值,否则返回-404;
if(IsEmpty() == -1){
int x = Array[0];
for(int j=0; j<MaxSize-1; j++)
Array[j] = Array[j+1];//前移
MaxSize--;
return x;
}
return -404;
}
//读取队列头部元素
public int GetFront(){
//读队头,若队列非空,则返回队列头元素的值,否则返回-404;
if(IsEmpty() == -1){
return Array[0];
}
return -404;
}
}
