手机
当前位置:查字典教程网 >编程开发 >编程语言综合 >编写一个JAVA的队列类
编写一个JAVA的队列类
摘要:根据这些特点,对队列定义了以下六种操作:enq(x)向队列插入一个值为x的元素;deq()从队列删除一个元素;front()从队列中读一个元...

根据这些特点,对队列定义了以下六种操作:

enq(x) 向队列插入一个值为x的元素;

deq() 从队列删除一个元素;

front() 从队列中读一个元素,但队列保持不变;

empty() 判断队列是否为空,空则返回真;

clear() 清空队列;

search(x) 查找距队首最近的元素的位置,若不存在,返回-1。

Vector类是JAVA中专门负责处理对象元素有序存储和任意增删的类,因此,用Vector

可以快速实现JAVA的队列类。

public class Queue extends java

public synchronized void enq(ob ject x) {

super.addElement(x);

}

public synchronized ob ject deq() {

/* 队列若为空,引发EmptyQueueException异常 */

if( this.empty() )

throw new EmptyQueueException();

ob ject x = super.elementAt(0);

super.removeElementAt(0);

return x;

}

public synchronized ob ject front() {

if( this.empty() )

throw new EmptyQueueException();

return super.elementAt(0);

}

public boolean empty() {

return super.isEmpty();

}

public synchronized void clear() {

super.removeAllElements();

}

public int search(ob ject x) {

return super.indexOf(x);

}

}

public class EmptyQueueException extends java

}

以上程序在JDK1.1.5下编译通过

【编写一个JAVA的队列类】相关文章:

如何提升编写的Swift代码质量

ThinkJS 2.1版本出来了,支持TypeScript且性能大幅提升

使用nodejs开发cli项目实例

ruby元编程之method_missing的一个使用细节

Python通过select实现异步IO的方法

C#实现百分比转小数的方法

Python OS模块常用函数说明

python抽象基类用法实例分析

Ruby一行代码实现的快速排序

介绍Python中的readline()方法的使用

精品推荐
分类导航