标题: [基础] 编写一个JAVA的队列类
ljjk5
元帅
Rank: 1


荣誉会员奖章
UID 46706
精华 1
积分 99426
帖子 49691
威望 554
金币 48489
热心 505
阅读权限 100
注册 2007-2-25
状态 离线
编写一个JAVA的队列类

 根据这些特点,对队列定义了以下六种操作:  enq(x) 向队列插入一个值为x的元素;
  deq() 从队列删除一个元素;
  front() 从队列中读一个元素,但队列保持不变;
  empty() 判断队列是否为空,空则返回真;
  clear() 清空队列;
  search(x) 查找距队首最近的元素的位置,若不存在,返回-1。
    Vector类是JAVA中专门负责处理对象元素有序存储和任意增删的类,因此,用Vector
  可以快速实现JAVA的队列类。 
     public class Queue extends java
    public synchronized void enq(Object x) {
    super.addElement(x);
    }
    public synchronized Object deq() {
    /* 队列若为空,引发EmptyQueueException异常 */
    if( this.empty() )
    throw new EmptyQueueException();
    Object x = super.elementAt(0);
    super.removeElementAt(0);
    return x;
    }
    public synchronized Object 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(Object x) {
    return super.indexOf(x);
    }
    }
   public class EmptyQueueException extends java
    }
  以上程序在JDK1.1.5下编译通过

网友 ljjk5 签名 - 网友社区 ===
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-8 05:43
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP