<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      編寫一個JAVA的隊列類

      [摘要]根據這些特點,對隊列定義了以下六種操作:     enq(x) 向隊列插入一個值為x的元素;     deq() 從隊列刪除一個元素;     front() 從隊列中讀一個元素,但隊列保持不變;     empty() 判斷隊列是否為空,空則返回真;     clear() 清空隊列;     ...
      根據這些特點,對隊列定義了以下六種操作:

          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下編譯通過



      日韩精品一区二区三区高清