欢迎来到山村网

编写一个JAVA的队列类

2019-03-02 14:45:26浏览:207 来源:山村网   
核心摘要:  根据这些特点,对队列定义了以下六种操作:  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() {

  

  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程序连接各种数据库的方法介绍

上一篇:

进程调度模拟程序

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com