데이터 구조 - 배열 시 뮬 레이 션 구현 대기 열
                                            
 21940 단어  데이터 구조 와 알고리즘
                    
대열 은 하나의
     로 배열 이나 링크 로 이 루어 질 수 있 으 며      원칙 을 따른다.2. 배열 시 뮬 레이 션 구현 대기 열
2.1 사고방식
대기 열 자체 에 서열 표 가 있 습 니 다. 배열 의 구 조 를 사용 하여 대기 열의 데 이 터 를 저장 하면:
maxSize 으로 설정 head 과 tail 대기 열의 앞 뒤 단 하 표를 각각 기록 해 야 합 니 다. head 는 데이터 의 출력 에 따라 달라 집 니 다. tail 은 데이터 의 입력 에 따라 달라 집 니 다 //      
class ArrayQueue{
    //        
    private int maxSize;
    //   
    private int head;
    //   
    private int tail;
    //      
    private int[] arr;
    //        
    public ArrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        //            
        head = -1;
        //             
        tail = -1;
    }
    //       
    //   true,    false
    public boolean isFull(){
        return tail == maxSize - 1;
    }
    //         ,      
    //   true,    false
    public boolean isEmpty(){
        return tail == head;
    }
    //       
    public void addQueue(int data){
        //  
        if(isFull()){
            System.out.println("    ");
            return;
        }
        //tail  
        tail ++;
        arr[tail] = data;
    }
    //      
    public int getData(){
        //  
        if(isEmpty()){
            //         ,throw       return
            throw new RuntimeException("    ");
        }
        head ++;
        return arr[head];
    }
    //        
    public void showQueue(){
        //  
        if(isEmpty()){
            System.out.println("    ");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("%d ",arr[i]);
        }
    }
    //      
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("    ");
        }
        return arr[head + 1];
    }
}
2.3. 문제 분석 과 해결
3.1 사고방식
class CircleArray{
    //        
    private int maxSize;
    //   
    private int head;
    //   
    private int tail;
    //      
    private int[] arr;
    public CircleArray(int arrayMaxSize){
        maxSize = arrayMaxSize;
        arr = new int[maxSize];
        //            
        head = 0;
        //             
        tail = 0;
    }
    //       
    //   true,    false
    public boolean isFull(){
        return (tail + 1) % maxSize == head;
    }
    //         ,      
    //   true,    false
    public boolean isEmpty(){
        return tail == head;
    }
    //       
    public void addQueue(int data){
        //  
        if(isFull()){
            System.out.println("    ");
            return;
        }
        //       
        arr[tail] = data;
        // tail  
        tail = (tail + 1) % maxSize;
    }
    //      
    public int getData(){
        //  
        if(isEmpty()){
            //         ,throw       return
            throw new RuntimeException("    ");
        }
        //      head           
        //  head             
        //head  
        //      
      int value = arr[head];
        head = (head + 1) % maxSize;
        return value;
    }
    //            
    public int size(){
        return (tail + maxSize - head) % maxSize;
    }
    //        
    public void showQueue(){
        //  
        if(isEmpty()){
            System.out.println("    ");
            return;
        }
        // head    
        for (int i = head; i < head + size(); i++) {
            System.out.printf("%d ",arr[i % maxSize]);
        }
    }
    //      
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("    ");
        }
        return arr[head];
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[JAVA] 배열 회전 출력요소 가 출력 을 시작 하 는 위치 에 주의 하 십시오. 모두 몇 라운드 의 수출 이 있 습 니까? n/2 + 1 매 라 운 드 는 상, 우, 하, 좌 로 나 뉜 다. 각 방향의 시작 위치 와 좌표 의 관 계 를 구...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.