순환 대기 열의 자바 구현

7221 단어 데이터 구조
import java.util.Arrays;

public  class LoopQueue {
    private int DEFAULT_SIZE = 10;
    //        。
    private int capacity;
    //                  
    private Object[] elementData;
    //               
    private int front = 0;
    private int rear = 0;

    //               
    public LoopQueue() {
        capacity = DEFAULT_SIZE;
        elementData = new Object[capacity];
    }

    //                
    public LoopQueue(T element) {
        this();
        elementData[0] = element;
        rear++;
    }

    public LoopQueue(T element, int initSize) {
        this.capacity = initSize;
        elementData = new Object[capacity];
        elementData[0] = element;
        rear++;
    }

    //          
    public int length() {
        if (empty()) {
            return 0;
        }
        return rear > front ? rear - front : capacity - (front - rear);
    }

    //     
    public void add(T element) {
        if (rear == front && elementData[front] != null) {
            throw new IndexOutOfBoundsException("       ");
        }
        elementData[rear++] = element;
        //   rear    ,    
        rear = rear == capacity ? 0 : rear;
    }

    //     
    public T remove() {
        if (empty()) {
            throw new IndexOutOfBoundsException("     ");
        }
        //      rear      
        T oldValue = (T) elementData[front];
        //      rear    
        elementData[front++] = null;
        //   front    ,    
        front = front == capacity ? 0 : front;
        return oldValue;
    }

    //        ,         
    public T element() {
        if (empty()) {
            throw new IndexOutOfBoundsException("     ");
        }
        return (T) elementData[front];
    }

    //             
    public boolean empty() {
        // rear==front rear     null
        return rear == front && elementData[rear] == null;
    }

    //       
    public void clear() {
        //            null
        Arrays.fill(elementData, null);
        front = 0;
        rear = 0;
    }

    public String toString() {
        if (empty()) {
            return "[]";
        } else {
            //   front < rear,      front rear     
            if (front < rear) {
                StringBuilder sb = new StringBuilder("[");
                for (int i = front; i < rear; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                int len = sb.length();
                return sb.delete(len - 2, len).append("]").toString();
            }
            //   front >= rear,     front->capacity  、0->front   
            else {
                StringBuilder sb = new StringBuilder("[");
                for (int i = front; i < capacity; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                for (int i = 0; i < rear; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                int len = sb.length();
                return sb.delete(len - 2, len).append("]").toString();
            }
        }
    }
}

좋은 웹페이지 즐겨찾기