양방향 순환 대기열 분석

1556 단어 Datastructure

양방향 순환 대기열은 순환 대기열의 기초 위에서
public class Deque {   
    private int maxSize;   
    private int left;   
    private int right;   
    private int nItems;   
    private long[] myDeque;   
    //constructor   
    public Deque(int maxSize){   
        this.maxSize = maxSize;   
        this.myDeque = new long[this.maxSize];   
        this.nItems = 0;   
        this.left = this.maxSize;   
        this.right = -1;   
    }   
    //insert a number into left side   
    public void insertLeft(long n){   
        if(this.left==0) this.left = this.maxSize;   
        this.myDeque[--this.left] = n;   
        this.nItems++;   
    }   
    //insert a number into right side   
    public void insertRight(long n){   
        if(this.right==this.maxSize-1) this.right = -1;   
        this.myDeque[++this.right] = n;   
        this.nItems++;   
    }   
    //remove from left   
    public long removeLeft(){   
        long temp = this.myDeque[this.left++];   
        if(this.left==this.maxSize) this.left = 0;   
        this.nItems--;   
        return temp;   
    }   
    //remove from right   
    public long removeRight(){   
        long temp = this.myDeque[this.right--];   
        if(this.left==-1) this.left = this.maxSize-1;   
        this.nItems--;   
        return temp;   
    }   
    //return true if deQue is empty   
    public boolean isEmpty(){   
        return (this.nItems==0);   
    }   
    //return size of the deQue   
    public int size(){   
        return this.nItems;   
    }   
}  

좋은 웹페이지 즐겨찾기