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
/* Array를 이용한 스택 구현
 * 1. 변수 : 배열, MAX 사이즈, top
 * 2. isEmpty : boolean, isFull : boolean
 * 3. input -> push : void
 * 4. output -> pop : data type of stack
 * */
 
public class Stack {
    private int MAX_SIZE;
    private int[] stack;
    private int top;
 
    public Stack() {
        MAX_SIZE = 5;
        stack = new int[MAX_SIZE];
        top = -1;
    }
 
    private boolean isEmpty() {
        return top == -1 ? true : false;
    }
    private boolean isFull() {
        return (top + 1 == MAX_SIZE) ? true : false;
    }
 
    public void push(int data) {
        if (!isFull())
            stack[++top] = data;
    }
 
    public int pop() {
        if (!isEmpty())
            return stack[top--];
        return -1;
    }
 
    public void display() {
        System.out.print("top : " + top + "\nstack : ");
        for (int idx = 0; idx <= top; idx++)
            System.out.print(stack[idx] + " ");
        System.out.println();
    }
}
cs

https://mailmail.tistory.com/26

+ Recent posts