0

ご挨拶、

配列がいっぱいになると拡張する循環配列を使用して Deque を実装しようとしています。私の問題は、配列が拡張を拒否しているようです。size() の計算が間違っているか、フロント インデックスとリア インデックスの更新方法に問題があります。私はそれを何度も見てきましたが、これを理解しているようです。誰でも助けることができますか?

public class ArrayDeque
    {
       public static final int INIT_CAPACITY = 8;   // initial array capacity
       protected int capacity;  // current capacity of the array
       protected int front;     // index of the front element
       protected int rear;      // index of the rear element
       protected int[] A;       // array deque

       public ArrayDeque( )      // constructor method
       {
          A = new int[ INIT_CAPACITY ];
          capacity = INIT_CAPACITY;
          front = rear = 0;
       }

        /**
         * Display the content of the deque
         */
        public void printDeque( )
        {
          for ( int i = front; i != rear; i = (i+1) % capacity )
              System.out.print( A[i] + " " );
              System.out.println();
        }
       /**
         * Returns the number of items in this collection.
         */
        public int size()
        {
            return (capacity - front + rear) % capacity;
        }
        /**
         * Returns true if this collection is empty.
         */ 
        public boolean isEmpty()
        {
            return front == rear;
        }
        /**
         * Returns the first element of the deque
         */
        public int getFirst() throws EmptyDequeException
        {     
            if(isEmpty()){
                throw new EmptyDequeException("Deque is empty.");
            }
            return A[front % capacity];       
        }
        /**
         * Returns the last element of the deque
         */
        public int getLast() throws EmptyDequeException
        {  
            if(isEmpty()){
                throw new EmptyDequeException("Deque is empty.");
            }
            return A[(rear - 1) % capacity];        
        }
        /**
         * Inserts e at the beginning (as the first element) of the deque
         * If array is full, extend array by doubling its capacity and insert element in new array
         */
        public void insertFirst(int e)
        {
            if(size() == capacity){
                int[] B = new int[2*capacity];
                for(int i = 0; i < size(); i++){
                    B[i] = A[i];
                }
                A = B;
            }
            int[] B = new int[capacity];
            for(int i = 0; i < size(); i++){
                B[i] = A[i];
            }
            A = B;
            for(int i = size(); i >= front; i--){
                A[i+1] = A[i];
            }
            A[front] = e;
            rear = (rear + 1) % capacity;
        }
        /**
         * Inserts e at the end (as the last element) of the deque
         * If array is full, extend array by doubling its capacity and insert element in new array
         */
        public void insertLast(int e)
        {
            if(size() == capacity){
                capacity *= 2;
                int[] B = new int[capacity];
                for(int i = 0; i < size(); i++){
                    B[i] = A[i];
                }
                A = B;
            }
            A[rear] = e;
            rear = (rear + 1) % capacity;
        }
        /**
         * Removes and returns the first element of the deque
         * Shrink array by half of current size N when number of elements in the deque falls below N/4
         * minimum capacity should always be 8
         */
        public int removeFirst() throws EmptyDequeException
        {
            if(isEmpty()){
                throw new EmptyDequeException("Deque is empty.");
            }
            else if(capacity >= 8){
                if(size() < capacity/4){
                    capacity /= 2;
                    int[] B = new int[capacity];
                    for(int i = 0; i < size(); i++){
                        B[i] = A[i];
                    }
                    A = B;
                }
            }
            int temp = A[front];
            A[front] = 0;
            front = (front + 1) % capacity;
            return temp;
        }
        /**
         * Removes and returns the last element of the deque
         * Shrink array by half of current size N when number of elements in the deque falls below N/4
         * minimum capacity should always be 8
         */
        public int removeLast() throws EmptyDequeException
        {
            if(isEmpty()){
                throw new EmptyDequeException("Deque is empty.");
            }
            else if(capacity >= 8){
                if(size() < capacity/4){
                    int[] B = new int[capacity/2];
                    for(int i = 0; i < capacity; i++){
                        B[i] = A[i];
                    }
                    A = B;
                }
            }
            int temp = A[rear - 1];
            A[rear] = 0;
            rear = (rear - 1) % capacity;
            return temp;
        }
    }  // end class

テスト入力:

for(i = 1; i <= 100; i++)
   q.insertLast(i);
   q.printDeque();

for(i = 1; i <= 99; i++)
   k = q.removeFirst();
   q.printDeque();

テスト出力: いくつかの印刷ステートメントを設定しましたが、何らかの理由でサイズが常に 7 のままです...

Exception in thread "main" A3.EmptyDequeException: Deque is empty.
    at A3.ArrayDeque.removeFirst(ArrayDeque.java:133)
    at A3.ArrayMain.main(ArrayMain.java:37)
4

2 に答える 2

0

さて、これを考慮してください...

最大容量が 8 の場合、キューには合計 9 つのサイズ状態 (0 1 2 3 4 5 6 7 および 8) があります。

ANY_NUMBER % 8 は、0 1 2 3 4 5 6 および 7 の 8 つの状態のみを持つことができます。

これは宿題なので(正直に言ってくれてありがとう)、すべてを台無しにしたくはありませんが、これは正しい方向に向けられるはずです. 幸運を!

于 2011-02-24T01:07:46.757 に答える
0

insertFirst メソッドを見て、配列がいっぱいになったときに何をするかを確認してください。最初の if ブロックだけでなく、メソッド全体を読み取ります。容量を変更したことはありますか?

于 2011-02-24T01:14:37.507 に答える