考试报名信息,太谷教育信息网欢迎您!
当前位置:首页 > > 考试 > > 计算机类 > 内容页

编写一个JAVA的队列类

2021-04-22 11:48:32计算机类访问手机版402

  编写一个JAVA的队列类

  如何编写一个JAVA的队列类?下面是由小编为大家整理的编写一个JAVA的队列类,喜欢的可以收藏一下!了解更多详情资讯,请关注应届毕业生考试网!

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

  enqx 向队列插入一个值为x的元素;

  deq 从队列删除一个元素;

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

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

  clear 清空队列;

  searchx 查找距队首最近的元素的`位置,若不存在,返回-1。

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

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

  public class Queue extends java

  public synchronized void enqob ject x

  super.addElementx;

  public synchronized ob ject deq

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

  if this.empty

  throw new EmptyQueueException;

  ob ject x = super.elementAt0;

  super.removeElementAt0;

  return x;

  public synchronized ob ject front

  if this.empty

  throw new EmptyQueueException;

  return super.elementAt0;

  public boolean empty

  return super.isEmpty;

  public synchronized void clear

  super.removeAllElements;

  public int searchob ject x

  return super.indexOfx;

  public class EmptyQueueException extends java

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

TAG标签: Java编写