小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

編寫一個JAVA的隊列類

 N_once 2007-07-30

  隊列是設計程序中常用的一種數據結構。它類似日常生活中的排隊現象,采用一種被稱為“先進先出”(LIFO)的存儲結構。數據

  隊列是設計程序中常用的一種數據結構。它類似日常生活中的排隊現象,采用一種被稱為“先進先出”(LIFO)的存儲結構。數據元素只能從隊尾進入,從隊首取出。在隊列中,數據元素可以任意增減,但數據元素的次序不會改變。每當有數據元素從隊列中被取出,后面的數據元素依次向前移動一位。所以,任何時候從隊列中讀到的都是隊首的數據。


  根據這些特點,對隊列定義了以下六種操作:
  enq(x) 向隊列插入一個值為x的元素;
  deq() 從隊列刪除一個元素;
  front() 從隊列中讀一個元素,但隊列保持不變;
  empty() 判斷隊列是否為空,空則返回真;
  clear() 清空隊列;
  search(x) 查找距隊首最近的元素的位置,若不存在,返回-1。

  Vector類是JAVA中專門負責處理對象元素有序存儲和任意增刪的類,因此,用Vector
  可以快速實現JAVA的隊列類。元素只能從隊尾進入,從隊首取出。在隊列中,數據元素可以任意增減,但數據元素的次序不會改變。每當有數據元素從隊列中被取出,后面的數據元素依次向前移動一位。所以,任何時候從隊列中讀到的都是隊首的數據。


  根據這些特點,對隊列定義了以下六種操作:
  enq(x) 向隊列插入一個值為x的元素;
  deq() 從隊列刪除一個元素;
  front() 從隊列中讀一個元素,但隊列保持不變;
  empty() 判斷隊列是否為空,空則返回真;
  clear() 清空隊列;
  search(x) 查找距隊首最近的元素的位置,若不存在,返回-1。

  Vector類是JAVA中專門負責處理對象元素有序存儲和任意增刪的類,因此,用Vector
  可以快速實現JAVA的隊列類。

 
import java.util.*;
public class BroadCastQueue extends Vector {//向量的子類
 public BroadCastQueue(){
  super();
 }
 
public synchronized void enq(Object x) {
super.addElement(x);
}
public synchronized Object deq() {
/* 隊列若為空,引發(fā)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);
}
}
 
import java.util.*;
public class BroadCastQueue extends Vector {//向量的子類
 public BroadCastQueue(){
  super();
 }
 
public synchronized void enq(Object x) {
super.addElement(x);
}
public synchronized Object deq() {
/* 隊列若為空,引發(fā)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);
}
}
 
 
import java.lang.*;
public class EmptyQueueException extends RuntimeException{
 public EmptyQueueException(){
  
 }
}

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多