JAVA_Learn/Do it! _ Algorithm Study

#9. 3주차 과제 2 [Chap04_4-3 ~ 4-4]

CEJ_0929 2022. 1. 11. 17:14

4-3. (IntQueue) + [B]+[C]+[D]+[E]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package practice;
 
public class IntQueue_4_3 {
    private int max;    //용량
    private int front;    //첫번째 커서 요소
    private int rear;    //마지막 요소
    private int num;    //현재 데이터 수
    private int[]que;    //큐 본체
    
    //==================예외처리======================
    public class EmptyIntQueueException extends RuntimeException{
        public EmptyIntQueueException() {}
    }
    
    public class OverflowIntQueueException extends RuntimeException{
        public OverflowIntQueueException() {}
    }
 
    //===========생성자에서 큐 생성========================
    public IntQueue_4_3(int capacity) {
        
        this.num = this.front = this.rear = 0;
        
        this.max = capacity;
        
        try {
            this.que = new int[max];    //que 본체 생성
        } catch (OutOfMemoryError e) {
            max = 0;    //생성할 수 없는 경우
        }
        
    }
    
    //========메서드 영역================================
    //---------4-3[B]-----------------------
    public int enque(int x) throws OverflowIntQueueException{
        
        if(num>=max)
            throw new OverflowIntQueueException();
        
        que[rear++= x;
        num++;
        
        if(rear == max)    rear = 0;
        
        return x;
    }
    
    //----------4-3[C]---------------------
    public int deque() throws EmptyIntQueueException{
        if(num<=0)
            throw new EmptyIntQueueException();
        
        int x = que[front++];
        num--;
        
        if(front==max)    front = 0;
        
        return x;
    }
    
    //---------4-3[D]---------------------
    public int peek() throws EmptyIntQueueException{
        if(num <= 0)
            throw new EmptyIntQueueException();
        return que[front];
    }
    
    
    public int indexOf(int x) {
        for (int i = 0; i < num; i++) {
            int idx = (i+front)%max;
            
            if(que[idx] == x) return idx;            
        }
        return -1;
    }
    
    public void clear() {
        num = front = rear = 0;
    }
    
    public int capacity() {
        return max;
    }
    
    public int size() {
        return num;
    }
    
    public boolean isEmpty() {
        return num<=0;
    }
    
    public boolean isFull() {
        return num>=max;
    }
    
    
    public void dump() {
        if(num<=0)
            System.out.println("큐가 비어 있습니다.");
        else {
            for (int i = 0; i < num; i++) {
                System.out.print(que[(i+front)%max]+" ");
            }
            System.out.println();
        }
    }
    
    
    
    
 
}
 
cs

4-4. (IntQueueTester)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package practice;
 
import java.util.Scanner;
 
 
public class IntQueueTester_4_4 {
    public static void main(String[] args) {
 
        Scanner stdIn = new Scanner(System.in);
        IntQueue_4_3 s = new IntQueue_4_3(64);
 
        while(true) {
            System.out.println("현재 데이터 수: "+s.size()+" / "+s.capacity());
            System.out.print("(1)인큐  (2)디큐  (3)피크  (4)덤프  (5)종료: ");
            int menu = stdIn.nextInt();
            if(menu==0break;
 
            int x;
            switch(menu) {
            case 1:
                System.out.print("인큐 할 데이터: ");
                x = stdIn.nextInt();
                try {
                    s.enque(x);
                } catch (IntQueue_4_3.OverflowIntQueueException e) {
                    System.out.println("que가 가득 찼습니다.");
                }
                break;
 
            case 2:
                try {
                    x = s.deque();
                    System.out.println("디큐한 데이터는 "+x+"입니다.");
                } catch (IntQueue_4_3.EmptyIntQueueException e) {
                    System.out.println("que가 비었습니다.");
                }
                break;
 
            case 3:
                try {
                    x = s.peek();
                    System.out.println("피크한 데이터는 "+x+"입니다.");
                } catch (IntQueue_4_3.EmptyIntQueueException e) {
                    System.out.println("que가 비었습니다.");
                }
                break;
 
            case 4:
                s.dump();
                break;
            }
        }
    }
 
}
 
cs