Javaプログラミング

トップページ      |      目次
←前へ      次へ→

    • キュー(queue)
      • キューとはでrear(後方)と呼ばれる一方の端点で挿入が行われ、front(前方)と呼ばれるもう一方の端点で削除が行われる順序付きリストとして定義される。 キューは以下の性質を持つ。
        • 先に格納されたデータが先に取り出され、後に格納されたデータは後に取り出される(この性質をFIFO:Last In First Out,あるいはLILO:Last in Last Outという)
        • データの出し入れはそれぞれenqueueとdequeueで行う
        • dequeueが行われるとfrontの要素が削除される
        • enqueueが行われるとrearの後に要素が追加され新たなrearの要素になる
        • キューが有限長でデータが一杯(full)の状態のキューにenqueueしようとするとオーバーフロー(overflow)が発生する、(通常は例外処理される)
        • データが空(empty)の状態のキューからdequeueしようとするとアンダーフロー(underflow)が発生する、(通常は例外処理される)
      • エンキュー(enqueue)とデキュー(dequeue)
      • 実行結果
      • リングバッファによる効率的なキューの実現
      • キューを配列を用いて実装する場合、デキューされた領域を再利用するためにリングバッファを用いた実装が行われる。 リングバッファとは領域の最後に到達するとその次は領域の最初を参照するような循環型に参照するような方法である。 実行結果
      • プログラムによる実装
      • 
                class Queue{
                  int front, rear;
                  int capacity;
                  int[] array;
                
                  public Queue(int size) {
                    this.capacity=size;
                    this.front=-1;
                    this.rear=-1;
                    array=new int[this.capacity];
                  }
                
                  public boolean isEmptyQueue(){
                    return this.front==-1?true:false;
                  }
                
                  public boolean isFullQueue(){
                    return (this.rear+1)%this.capacity==this.front?true:false;
                  }
                  public int queueSize() {
                    if(this.rear==-1)return 0;
                    int k=0;
                    int p=this.front;
                    while((p++)%this.capacity!=this.rear){
                      k++;
                    };
                    return k+1;
                  }
                  public void enQueue(int data) {
                    if(isFullQueue()) {
                      System.out.println("Queue Overflow");
                    }else {
                      System.out.println(data+" is Enqueued");
                      this.rear=(this.rear+1)%this.capacity;
                      this.array[this.rear]=data;
                      if(this.front==-1) {
                        this.front=this.rear;
                      }
                    }
                  }
                  public int deQueue() {
                    int data=0;
                    if(isEmptyQueue()) {
                      System.out.println("Queue is Empty");
                    }else {
                      data=this.array[this.front];
                      System.out.println(data+" is Dequeued");
                      if(this.front==this.rear) {
                        this.front=-1;
                        this.rear=-1;
                      }else {
                        this.front=(this.front+1)%this.capacity;
                      }
                    }
                    return data;
                  }
                
                }
                
                public class QueueTest {
                  public static void main(String[] args) {
                    Queue Q=new Queue(3);
                
                    Q.enQueue(1);
                    Q.enQueue(2);
                    Q.enQueue(3);
                    Q.enQueue(4);
                
                    Q.deQueue();
                    Q.deQueue();
                    Q.deQueue();
                    Q.deQueue();
                
                    //Q.enQueue(4);
                    //Q.enQueue(5);
                
                    System.out.println(Q.queueSize());
                
                
                  }
                }
              


    ←前へ      次へ→